简体   繁体   English

如何使用X11获取当前光标位于其上的窗口?

[英]How do I get the window that currently has the cursor on top of it with X11?

How can I retrieve the top window of which the cursor is on top of in the X11 server? 如何检索光标位于X11服务器顶部的顶部窗口?

The window doesn't have to be ”active” (selected, open, whatever), it just has to have the cursor floating on top of it. 窗口不必是“活动的”(选择,打开,无论如何),它只需要将光标悬浮在它上面。

Thanks in advance. 提前致谢。

You can use XQueryPointer() to get the mouse position. 您可以使用XQueryPointer()来获取鼠标位置。 Then get a window list using XQueryTree() . 然后使用XQueryTree()获取一个窗口列表。 XQueryTree() returns the window list in proper z-order so you can just loop through all the windows until you find one whose bounding box is under the pointer, XGetWindowAttributes() will give you everything you need to figure out the bounding box. XQueryTree()以正确的z顺序返回窗口列表,因此您可以遍历所有窗口,直到找到其边界框位于指针下的窗口, XGetWindowAttributes()将为您提供找出边界框所需的一切。 I'm not sure what you would do with shaped windows though. 我不知道你会用塑造窗户做什么。

I haven't work with X11 for a few years so this might be a rather clunky approach but it should work. 我有几年没有使用X11,所以这可能是一个相当笨重的方法,但它应该工作。 I also don't have my O'Reilly X11 books anymore, you'll want to get your hands on book one of that series if you're going to work with low level X11 stuff; 我也没有我的O'Reilly X11书籍了,如果你打算使用低级别的X11,那么你会想要把它放在书中。 I think the whole series is available for free online these days. 我认为这几天全系列免费在线提供。

I haven't programmed X11 for over a decade, so forgive me if I get this wrong. 我已经十多年没有对X11进行编程了,所以如果我弄错了,请原谅我。

I believe you can register for mouse movement events on your windows. 我相信你可以在你的窗户上注册鼠标移动事件。 If you handle such event by storing the window handle in some variable or other, and then handling the event so it doesn't percolate down the tree, then at the time you want to identify the window you can just query the variable. 如果通过将窗口句柄存储在某个变量或其他变量中来处理此类事件,然后处理事件以使其不会向下渗透树,那么在您想要识别窗口时,您只需查询变量即可。

However this will only work when the mouse is over a window you have registered a suitable event handler for, so you won't know about windows belonging to other applications - unless there is a way to register for events on other people's windows which may be possible. 但是,只有在鼠标位于已注册合适事件处理程序的窗口上时,这才会起作用,因此您不会知道属于其他应用程序的窗口 - 除非有办法在其他人的窗口上注册事件,这可能是可能。

The advantage over the other answer is that you don't have to traverse the whole tree. 优于其他答案的优点是您不必遍历整个树。 The disadvantage is that you need to handle a great many mouse movement events, and it may not work to find other people's windows. 缺点是你需要处理很多鼠标移动事件,它可能无法找到其他人的窗口。

I believe there may also be mouse enter and mouse leave events too which would reduce the amount of processing required. 我相信也可能有鼠标输入和鼠标离开事件,这将减少所需的处理量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM