How to get the window in which the cursor is currently above it using X11?

How to get the top window in which the cursor is on top of the X11 server?

The window should not be "active" (selected, open, any), it should have a cursor floating on top of it.

Thanks in advance.

+7
source share
2 answers

You can use XQueryPointer() to get the mouse position. Then get a list of windows using XQueryTree() . XQueryTree() returns the list of windows in the correct z order, so you can simply scroll through all the windows until you find the one whose bounding box is below the pointer, XGetWindowAttributes() will provide you with everything you need to define the bounding box. I'm not sure what you would do with formatted windows.

I have not been working with X11 for several years, so this may be a rather clumsy approach, but it should work. I also no longer have my O'Reilly X11 books, you need to pick up the book in one of these series if you intend to work with low-level X11 materials; I think the entire series is available free online these days.

+4
source

I have not programmed X11 for more than ten years, so forgive me if I am wrong.

I believe that you can register for mouse movement events in your windows. If you handle such an event by storing the window handle in some variable or another, and then processing the event so that it does not leak down the tree, then at the time you want to identify the window, you can simply request the variable.

However, this will only work when the mouse is above the window on which you registered a suitable event handler, so you will not know about windows belonging to other applications - if there is no way to register for events in other people, which is possible.

The advantage over the other answer is that you do not have to go through the whole tree. The disadvantage is that you need to handle a large number of mouse movement events, and may not work to find other user windows.

I believe that there may also be mouse and mouse input events that will reduce the required processing.

+3
source

All Articles