# How to catch the plugin window focus loss event in mac OS?

**URL:** <https://iplug2.discourse.group/t/how-to-catch-the-plugin-window-focus-loss-event-in-mac-os/920>\
**Category:** macOS\
**Created:** [15 July 2024 10:22 UTC](https://iplug2.discourse.group/t/how-to-catch-the-plugin-window-focus-loss-event-in-mac-os/920 "2024-07-15T10:22:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasyan](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/vasyan/32/82_2.png) [@vasyan](https://iplug2.discourse.group/u/vasyan)\
**Post date:** [15 July 2024 10:22 UTC](https://iplug2.discourse.group/t/how-to-catch-the-plugin-window-focus-loss-event-in-mac-os/920/1 "2024-07-15T10:22:44Z")

</div>

I made a drop-down list of presets with the ability to specify favorites.

![image](https://global.discourse-cdn.com/free1/uploads/iplug2/original/1X/1dc2029c5d15435d6ad79838ada7ede258ad80bc.png)

It closes when you select a preset, or by clicking the mouse to any other place in the plugin window. I would also like the list to close also when the focus of the plugin window is lost. If in Windows this is solved by reacting to the `WM_KILLFOCUS` message, then in mac OS, unfortunately, I do not know how to implement it.  
I would appreciate any help on this issue.

---

<div class="post-metadata">

**Author:** ![vasyan](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/vasyan/32/82_2.png) [@vasyan](https://iplug2.discourse.group/u/vasyan)\
**Post date:** [18 July 2024 05:03 UTC](https://iplug2.discourse.group/t/how-to-catch-the-plugin-window-focus-loss-event-in-mac-os/920/2 "2024-07-18T05:03:04Z")

</div>

However, not everything turned out as well as we would like. I had to add the code to some files:

1. IGraphicsMac\_view.h:

```auto
@interface IGRAPHICS_VIEW : NSView <NSTextFieldDelegate/*, WKScriptMessageHandler*/>
{
  ...
}
- (void) windowDidResign: (NSNotification*)notification;
- (BOOL) resignFirstResponder;

```

1. IGraphicsMac\_view.mm:

```auto
- (void) viewDidMoveToWindow
{
  NSWindow* pWindow = [self window];
  
  if (pWindow)
  {
   ...   
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(windowDidResign:)
                                                 name:NSWindowDidResignKeyNotification
                                               object:pWindow];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(windowDidResign:)
                                                 name:NSWindowDidResignMainNotification
                                               object:pWindow];
}

- (void) windowDidResign: (NSNotification*)notification
{
  mGraphics->OnLostFocus();
}

- (BOOL) resignFirstResponder
{
  mGraphics->OnLostFocus();
  return YES;
}

```

1. A new method has also been added to the IGraphics and IControl classes: `void OnLostFocus()`, which reacts to the loss of focus by the plugin window. In my case, pop-up lists and pop-up menus are hidden.  
In principle, everything works as desired - when switching focus, the list closes, however, if any pop-up menu appears on the screen, such as in the screenshot below, then none of my methods work. I would like to track the appearance of any other pop-up menus on the screen and get a loss of focus event, but alas, I am not such a big expert on macOS, so I ask for help from knowledgeable colleagues.

 ![Снимок экрана 2024-07-18 в 10.56.46](https://global.discourse-cdn.com/free1/uploads/iplug2/original/1X/9b4f8139ecb5b8fe85df34047e3ee6c03b11c75f.png)

---

<div class="post-metadata">

**Author:** ![vasyan](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/vasyan/32/82_2.png) [@vasyan](https://iplug2.discourse.group/u/vasyan)\
**Post date:** [18 July 2024 07:04 UTC](https://iplug2.discourse.group/t/how-to-catch-the-plugin-window-focus-loss-event-in-mac-os/920/3 "2024-07-18T07:04:45Z")

</div>

I also want to inform you that this topic is relevant for all those who use **IPopupMenuControl** , since this menu does not disappear from the screen when changing focus, and this, in turn, is not good (the standard pop-up menu always disappears from the screen when deactivated).

---

<div class="post-metadata">

**Author:** ![olilarkin](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/olilarkin/32/2_2.png) [@olilarkin](https://iplug2.discourse.group/u/olilarkin)\
**Post date:** [23 July 2024 20:15 UTC](https://iplug2.discourse.group/t/how-to-catch-the-plugin-window-focus-loss-event-in-mac-os/920/4 "2024-07-23T20:15:17Z")

</div>

Thanks for this. We should add it. For future reference, it’s best to make an issue on github and a PR, rather than post code here.

here is a work in progress

> <https://github.com/iPlug2/iPlug2/pull/1114>
>
> Addresses #1113

---

<div class="post-metadata">

**Author:** ![vasyan](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/vasyan/32/82_2.png) [@vasyan](https://iplug2.discourse.group/u/vasyan)\
**Post date:** [24 July 2024 09:26 UTC](https://iplug2.discourse.group/t/how-to-catch-the-plugin-window-focus-loss-event-in-mac-os/920/5 "2024-07-24T09:26:16Z")

</div>

Thanks Oli. However, there are some nuances that have not been fully tested on different operating systems and DAWs. In particular, I recorded a drop in DAW at closing. As I found out, the reason for the crash is sending a notification NSWindowDidResignKeyNotification at the time of destroy of controls. I decided to make a check for the presence of a timer in the IGraphicsMac\_view.mm file:

```auto
- (void) windowDidResign: (NSNotification*)notification
{
  if (mTimer == nil)
    return;
  mGraphics->OnLostFocus();
}

```

In this case, the OnLostFocus() event is not sent.

---

<div class="post-metadata">

**Author:** ![vasyan](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/vasyan/32/82_2.png) [@vasyan](https://iplug2.discourse.group/u/vasyan)\
**Post date:** [24 July 2024 09:28 UTC](https://iplug2.discourse.group/t/how-to-catch-the-plugin-window-focus-loss-event-in-mac-os/920/6 "2024-07-24T09:28:47Z")

</div>

You also need to add the method to IControl:

```auto
/** This is an called when focus is lost. /todo check this */
  virtual void OnLostFocus() {}

```

And in class IGraphics -  
to IGraphics.h:  
`void OnLostFocus();`  
to IGraphics.cpp:

```auto
void IGraphics::OnLostFocus()
{
  TRACE
  ForAllControls(&IControl::OnLostFocus);
}

```

In IGraphicsWin.cpp add WM\_KILLFOCUS message processing like this:

```auto
case WM_KILLFOCUS:
    {
      pGraphics->OnLostFocus();
      return 0;
    }

```

---

<div class="post-metadata">

**Author:** ![vasyan](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/vasyan/32/82_2.png) [@vasyan](https://iplug2.discourse.group/u/vasyan)\
**Post date:** [25 July 2024 01:46 UTC](https://iplug2.discourse.group/t/how-to-catch-the-plugin-window-focus-loss-event-in-mac-os/920/7 "2024-07-25T01:46:27Z")

</div>

> [@olilarkin](#):
>
> here is a work in progress

This makes me happy. The main thing is that IControl includes a new virtual method **OnLostFocus()**, which can be overridden in user control, for example, as I did in my DReverb2 plugin menu, so that if focus is lost, menu can disappear from the screen.

![image](https://global.discourse-cdn.com/free1/uploads/iplug2/original/1X/8737e8ce4bb338d6e8477c1064e68cf1cc27562e.png)

---

<div class="post-metadata">

**Author:** ![TBProAudio](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/tbproaudio/32/111_2.png) [@TBProAudio](https://iplug2.discourse.group/u/TBProAudio)\
**Post date:** [23 September 2024 05:10 UTC](https://iplug2.discourse.group/t/how-to-catch-the-plugin-window-focus-loss-event-in-mac-os/920/8 "2024-09-23T05:10:59Z")

</div>

Hi Oli,

is there a reason why

```auto
if (mTimer == nil)
    return;

```

was not considered in the PR above?  
Or is this covered elsewhere?

At least I need this hack to avoid a repeated crash with MAC M1…  
Thank you!

---

<div class="post-metadata">

**Author:** ![vasyan](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/vasyan/32/82_2.png) [@vasyan](https://iplug2.discourse.group/u/vasyan)\
**Post date:** [8 October 2024 05:30 UTC](https://iplug2.discourse.group/t/how-to-catch-the-plugin-window-focus-loss-event-in-mac-os/920/9 "2024-10-08T05:30:45Z")

</div>

I suggested this code to Ole. Simply by the presence of a timer, you can determine the validity of the object and leave the function at the end of work in order to avoid errors.  
Unfortunately, I haven’t tested this code on MAC M1, but it works fine on MAC x86-64.

---

<div class="post-metadata">

**Author:** ![TBProAudio](https://yyz2.discourse-cdn.com/free1/user_avatar/iplug2.discourse.group/tbproaudio/32/111_2.png) [@TBProAudio](https://iplug2.discourse.group/u/TBProAudio)\
**Post date:** [8 October 2024 05:52 UTC](https://iplug2.discourse.group/t/how-to-catch-the-plugin-window-focus-loss-event-in-mac-os/920/10 "2024-10-08T05:52:16Z")

</div>

We tested on Sequoia M1. The addition fixes menu related crashes.  
Thank you for this!!
