2

Is there an XAppFocusOut event similar to Windows WM_ACTIVATEAPP or OSX's applicationDidResignActive or some other way to get notified when an app loses focus? XCB solution preferred.

To clarify: I'm interested in an event when the app, not a window loses focus.

Thank you.

  • possible duplicate of [FocusIn/FocusOut not generated](http://stackoverflow.com/questions/18234136/focusin-focusout-not-generated) – Eugene Sh. Jul 15 '15 at 18:17
  • how is that a duplicate? I'm interested in an event for when the _app_ not the _window_ loses focus –  Jul 15 '15 at 18:19
  • X does not have (or understand) the concept of "apps" as distinct from windows. It does have a concept of "clients" with connections -- any given window belongs to a particular client and will be cleaned up if the client (connection) goes away, but clients do not have focus or anything else associated directly with them. – Chris Dodd Jul 15 '15 at 19:04
  • @ChrisDodd I know but I still need to know when a window loses focus _not_ because another window that I know of (that I created) gains focus. –  Jul 15 '15 at 19:08
  • @EugeneSh. - It doesn't seem to be a dupe to me - that question was narrowly focused on how an X server running without a window manager dealt with focus. – Andrew Henle Jul 15 '15 at 20:12
  • 1
    The best you can do is keep track of all your own windows yourself. When ALL the windows in your app has focused out then the app is out of focus. I'm not sure but I think you may need to include a timeout as well to catch cases when you receive focusout before focusin – slebetman Jul 15 '15 at 20:16
  • @slebetman good idea - after a window is deactivated, I start a countdown timer: if another window becomes active before the timeout then the timer is defused, otherwise an app_deactivated event is triggered and the timer is defused. If you post that I'll accept the answer (seems a reasonable choice). –  Jul 16 '15 at 21:11

1 Answers1

3

You want the FocusOut X event.

The X server can report FocusIn or FocusOut events to clients wanting information about when the input focus changes. The keyboard is always attached to some window (typically, the root window or a top-level window), which is called the focus window. The focus window and the position of the pointer determine the window that receives keyboard input. Clients may need to know when the input focus changes to control highlighting of areas on the screen.

To receive FocusIn or FocusOut events, set the FocusChangeMask bit in the event-mask attribute of the window.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • That tells you when a window loses focus. I'm interested in the event when a window loses focus as the result of focusing say, an icon on the desktop, or another window _from a different app_, but _not_ as a result of switching between windows of my own app. –  Jul 15 '15 at 21:53
  • 1
    @cap - To do that, you're going to have to reliably track ALL windows created by that application. Some ideas on how to do that are here: http://unix.stackexchange.com/questions/5478/what-process-created-this-x11-window – Andrew Henle Jul 15 '15 at 22:13
  • I've found FocusOut to be unreliable under Kwin 4.x, so I used a different solution: [monitor _NET_ACTIVE_WINDOW by subscribing to PropertyNotify events on the root window](http://stackoverflow.com/a/41426283/435253). – ssokolow Jan 02 '17 at 12:08