简体   繁体   English

Xlib,C ++-获取窗口句柄(SendEvent,…)

[英]Xlib,C++ - Get Window Handle (SendEvent,…)

I am currently working with XLib in order to code an Application that can send Mouse and Keyboard Events to a certain window (Not display). 我目前正在与XLib一起使用,以便对可以将鼠标和键盘事件发送到某个窗口 (不显示)的应用程序进行编码。

I was able to send these Events to a window using The "window"-handle obtained by the "XGetInputFocus()" method. 我能够使用“ XGetInputFocus()”方法获得的“窗口”句柄将这些事件发送到窗口。 Unfortunately I need to get the window-handle of another window (not the focused one) and send fake Input Events to that window. 不幸的是,我需要获取另一个窗口(而不是焦点窗口)的窗口句柄,并将伪造的Input Events发送到该窗口。 Therefore I wrote a method "enumerateWindows" to obtain this handle. 因此,我编写了一个方法“ enumerateWindows”来获取此句柄。

Here comes the problem. 问题来了。 I can find several windows, that are called the same. 我可以找到几个相同的窗口。 Like at least 3 "eclipse" windows in the tree. 像树上至少3个“日食”窗口。 Why is that? 这是为什么? Because I didn't know which "Window" to pick, I just compared all of them to the Window I obtained from the "XGetInputFocus()" Method. 因为我不知道要选择哪个“窗口”,所以我只将它们与从“ XGetInputFocus()”方法获得的窗口进行了比较。 There was no match (when comparing with the object identity ). 没有匹配项(与对象标识进行比较时)。 Nowhere in the tree. 在树上无处。 Why is that? 这是为什么? How else can I get the same Window reference that I get from "XGetInputFocus()"? 我还能如何获得与从“ XGetInputFocus()”获得的相同的Window引用?

Here is my Code and the Output: 这是我的代码和输出:

 #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;
}

I appreciate your help! 我感谢您的帮助! Thank you in advance! 先感谢您!

Regards 问候

Okay, I found the answer. 好吧,我找到了答案。 This Code works: 该代码有效:

Window *getWindowList(Display *disp, unsigned long *len) {
    Atom prop = XInternAtom(disp,"_NET_CLIENT_LIST",False), type;
    int form;
    unsigned long remain;
    unsigned char *list;

    if (XGetWindowProperty(disp,XDefaultRootWindow(disp),prop,0,1024,False,33,
                &type,&form,len,&remain,&list) != Success) {  // XA_WINDOW
        return 0;
    }

    return (Window*)list;
}
char *getWindowName(Display *disp, Window win) {
    Atom prop = XInternAtom(disp,"WM_NAME",False), type;
    int form;
    unsigned long remain, len;
    unsigned char *list;


    if (XGetWindowProperty(disp,win,prop,0,1024,False,AnyPropertyType,
                &type,&form,&len,&remain,&list) != Success) { // XA_STRING

        return NULL;
    }

    return (char*)list;
}
int main(){
    int i;
    unsigned long len;
    XKeyEvent esend;
    Display *disp = XOpenDisplay(NULL);
    Window *list;
    char *name;

        list = (Window*)getWindowList(disp,&len);
    for (i=0;i<(int)len;i++) {
        name = getWindowName(disp,list[i]);
        cout << i << ": " << name << endl;
        free(name);
        }
}

XLib Window Name Problems XLib窗口名称问题

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

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