【发布时间】:2015-07-21 06:43:58
【问题描述】:
我目前正在使用 XLib 编写一个可以将鼠标和键盘事件发送到某个窗口(不显示)的应用程序。
我能够使用“XGetInputFocus()”方法获得的“窗口”句柄将这些事件发送到窗口。不幸的是,我需要获取另一个窗口(不是焦点窗口)的窗口句柄并将假输入事件发送到该窗口。因此我写了一个方法“enumerateWindows”来获取这个句柄。
问题来了。我可以找到几个窗口,它们的名称相同。就像树中至少 3 个“日食”窗口。这是为什么? 因为我不知道要选择哪个“窗口”,所以我只是将它们都与我从“XGetInputFocus()”方法获得的窗口进行了比较。不匹配(当与对象 identity 进行比较时)。树上无处可去。这是为什么?我还能如何获得与“XGetInputFocus()”相同的窗口引用?
这是我的代码和输出:
#define EMPTY_WINDOW NULL
/* rootWindow: Window that is being looked from
* toSearch : Window that I want to obtain
*/
Window* enumerateWindows(Display *display, Window rootWindow, Window toSearch)
{
Window parent;
Window *children;
unsigned int nNumChildren;
char *name;
// Compare Object identity !!
if(&rootWindow == &toSearch){
cout << "Found correct Window!!" << endl;
return &rootWindow;
}
// Descend tree..
int status = XQueryTree(display, rootWindow, &rootWindow, &parent, &children, &nNumChildren);
if (status == 0)
{
cout << "Cant query further.." << endl;
return EMPTY_WINDOW;
}
if (nNumChildren == 0)
{
return EMPTY_WINDOW;
}
for (int i = 0; i < nNumChildren; i++)
{
Window *ret = enumerateWindows(display, children[i],toSearch);
if(ret != EMPTY_WINDOW ){
return ret;
}
}
XFree((char*) children);
return EMPTY_WINDOW;
}
int main()
{
// Obtain the X11 display.
Display *display = XOpenDisplay(0);
if(display == NULL) return -1;
// Get the root window for the current display.
Window winRoot = XDefaultRootWindow(display);
// Find the window which has the current keyboard focus.
Window winFocus;
int revert;
// The important part: Obtain Current focus window!
XGetInputFocus(display, &winFocus, &revert);
// Try to obtain the same window from the QueryTree
Window *win = enumerateWindows(display, winRoot, winFocus);
if( win == &winFocus){
cout << "Object Identity same! Problem solved!" << endl;
}else{
cout << "Didn't find the correct object.." << endl;
}
XCloseDisplay(display);
return 0;
}
感谢您的帮助!提前谢谢!
问候
【问题讨论】:
标签: c++ linux keyboard mouse capture