How to detect the "activation" of an Electron window on MacOS via CMD + TAB?


#1

I have a MacOS app that defines a key combo shortcut. When pressed, the app hides / shows itself via window.hide() / window.show(). Imagine for a moment that the app has been hidden via this shortcut. If the user clicks the app dock icon, I know to show the app, because I am watching for the app’s activate event. But… so far as I can tell, no similar event exists for when the user brings the app into “focus” via CMD + TAB. When I do this, the app’s menu is given focus at the top of the screen, but I’m unable to detect an event that would tell me that I need to show the window again.

Has anyone else dealt with this before?


#2

@tkambler hey, I faced the same issue, did you figured out it somehow?


#3

No, unfortunately. If you do, would you please let me know?


#4

i created an issue hope somebody will help us :slight_smile: https://github.com/electron/electron/issues/9577


#5

app.on(“browser-window-focus”, () => {
//This occurs when any window in the app gains focus.
}


#6

This does not appear to work.


#7

@andrewfan I have the answer we’ve been looking for. For reference, see this issue.

The trick is to not hide your application’s window, but rather to hide the entire application itself via Window.sendActionToFirstResponder:

    globalShortcut.register('CommandOrControl+Alt+X', () => {
        if (mainWindow.isVisible()) {
            Menu.sendActionToFirstResponder('hide:');
        } else {
            mainWindow.show();
        }
    });