How to know which window has focus and how to change it?

How to know which window has focus and how to change it?

windows focus

I would like to know how can I ask X11 which windows has focus. And if for any reason my own application (that may be visible or not) got the focus I want be able to let the former windows to get focus again.

For instance, my application is running with many others (e.g. firefox, gvim, nautilus,...)

Suppose that at first firefox has focus and that the user clicked on my app which now has the focus. I want that my application put focus on firefox again.

Does anyone knows how to achieve this? Books recommendations would be very nice.

Thanks a lot.

Use this XQueryTree to find the currently active, or top-most window.

Here is a function, when given a display, it will find the current window in focus:

static Window
Display *d;
Window foo;
Window win;
int bar;

    (void) XQueryPointer(d, DefaultRootWindow(d), &foo, &win,
        &bar, &bar, &bar, &bar, &bar);
    } while(win <= 0);

#ifdef VROOT
    int n;
    Window *wins;
    XWindowAttributes xwa;

    (void) fputs("=xwa=", stdout);

    /* do{  */
        XQueryTree(d, win, &foo, &foo, &wins, &n);
    /* } while(wins <= 0); */
    while(--n >= 0) {
        XGetWindowAttributes(d, wins[n], &xwa);
        if( (xwa.width * xwa.height) > bar) {
        win = wins[n];
        bar = xwa.width * xwa.height;

I found the source:

Good Luck

x11, Use this XQueryTree to find the currently active, or top-most window. Here is a function, when given a display, it will find the current window in focus: The focus() method sets focus to the current window. Tip: Use the blur() method to remove focus from the current window. Note: This method makes a request to bring the current window to the foreground. It may not work as you expect in all browsers, due to different user settings.

Take a look at the _NET_ACTIVE_WINDOW value of the root window which is set by most modern window managers:

xprop -root _NET_ACTIVE_WINDOW

This value can, of course, be obtained using Xlib library calls.

How can I tell which window has the keyboard focus, by looking , Or better, some setting that changes the color of the active window's title bar to be different than inactive windows, like it used to be? This thread is  When I look at the visual appearance of windows, the difference between the one that has focus and the ones that don't is very slight: the focused window has a slightly different shade of gray in the title bar, and the three round buttons at the upper-left are red, yellow, and green instead of gray.

You probably want the XGetInputFocus call.

Window focused;
int revert_to;

XGetInputFocus(dpy, &focused, &revert_to);

In this snippet, focused will be the window with current input focus, getting keyboard events and mouse button presses.

This will work even if the window manager does not set the _NET_ACTIVE_WINDOW property on the root window, as specified by EWMH. A few window managers, such as dwm and my 9wm, don't set this.

Document.hasFocus(), The hasFocus() method of the Document interface returns a Boolean value be used to determine whether the active element in a document has focus. within a popup window that is not the foreground doesn't have focus. The Javascript is supposed to check if the browser has focus and if it does dont change title, but if it doesnt, change to NEW MESSAGE!. This is msg(); do you see any reasons why it doesnt work?

I recommend an application called XDoTool. It supports quite a lot of queries, controls, and even hooks.

> xdotool getwindowfocus               # 29360135
> xdotool getwindowfocus getwindowpid  # 12988
> xdotool getwindowfocus getwindowname # tilda
> xdotool getwindowfocus behave '%@' blur getmouselocation
#      or focus, mouse-enter, etc.
x:514 y:317 screen:0 window:56623121
x:271 y:26 screen:0 window:56623121

Commands like behave accept a callback, which can be built-in like getmouselocation or external like exec notify-send 'focused window', exec zsh myscript.zsh, etc., however you want to use it.

Edit - you can focus using xdotool windowfocus [options] [window], as in xdotool search --class firefox windowfocus. In my case this causes errors because Firefox shows up as a couple dozen 'windows', but all have the same PID; it works given the right ID. Hopefully that's a start.

Edit 2 - the 'window ID' is the decimal representation of the window pointer, e.g. from xprop:

> xprop -root _NET_ACTIVE_WINDOW
_NET_ACTIVE_WINDOW(WINDOW): window id # 0x1c00007, 0x0
> xdotool getwindowfocus
> printf '%d\n' '0x1c00007'

How to detect a background program (in Windows 7) which steals , I don't want to change focus behavior using registry tweak. Note: Windows Process Explorer can be downloaded as part of Windows Sysinternals library. update: theres's any nice pogramm showing/logging, what process has focus: and left the PC running over night to find the program that was stealing the focus. Is there anyway in AutoIt to tell which window (GUI) is on top (has focus)? I can set the focus in AutoIt, but there does not seem to be any way to detect when the user has changed focus. It is possible to check for mouse clicks and check where the mouse was located when it was clicked, but this

Core Java 2: Fundamentals, There is at most one component in a window that has focus. You can change the focus traversal order; see the next chapter for more on this subject  A label at the bottom of the window describes the Picture that has the focus. Move the focus to another Picture by using Tab or Shift-Tab, or by clicking an image. Because a property change listener has been registered on the keyboard focus manager, the change in focus is detected and the label is updated appropriately.

Programming with MotifTM, When a window has focus, mum indicates that fact by changing the color of the window's frame.” When the focus changes, two informational events are sent by  This is not true. onResume gets called before the window has focus. If you bring up your activity and then put your phone to sleep, when you turn on the power button, your activity's onResume will be called, and you will be staring at the lock screen.

How to tell if app/page has focus (Example), However, it only works when the user switches tabs inside the browser window. If the browser window itself loses focus (while your tab is selected)  Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user. Then I read the docs for onWindowFocusChanged(boolean) , which says: Called when the current Window of the activity gains or loses focus .