Find the answer to your Linux question:
Results 1 to 4 of 4
hi .. i have a problem with this program ,it lists the current window along with window id thet are running on the system,result is that for a particular window ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    14

    Exclamation some keyboard/mouse events do not come back-xlib programming


    hi ..
    i have a problem with this program ,it lists the current window along with window id thet are running on the system,result is that for a particular window id that i enter i got odd result:
    for application like firefox or gedit just motion notify event works,non of the other events work??
    for my terminal(bash) everything works,key press,keyrelease,mouse notify,mouserelease,...
    i dont know why??

    //yhis part get the window id

    Code:
            Display * dis;
            Window w;
            dis = XOpenDisplay (0);
            w=XDefaultRootWindow(dis);
            Atom a = XInternAtom(dis, "_NET_CLIENT_LIST" , 1);
            Atom actualType;
            int format;
            unsigned long numItems, bytesAfter;
            unsigned char *data =0;
            int status = XGetWindowProperty(dis,w,a,0L,(~0L),0,AnyPropertyType,&actualType,&format,&numItems,&bytesAfter,&data);
    
            if (status >= Success && numItems)
            {
                    int *array = (int*) data;
                    int k;
                    for (k = 0; k < numItems; k++)
                    {
                            // get window Id:
                            Window w = (Window) array[k];
                            XSelectInput (dis, w, KeyPressMask |PointerMotionMask |ButtonReleaseMask);
                            char* name = '\0';
                            status = XFetchName(dis, w, &name);
                            if (status >= Success)
                            {
                                    printf("Found: %u  %s\n", w, name);
                             }
                            XFree(name);
        
                    }
                    XFree(data);
            }
    //Event handling for windows

    Code:
       
       XEvent event;
         while (1)  {
         XNextEvent(dis, &event);
         switch  (event.type) {
              
                //MOUSE LOCATION
                case MotionNotify:
                    fprintf(stdout,"x: %d y:%d\n",event.xmotion.x,event.xmotion.y);
                break;    
                
                //KEYBOARD KEY(it does not wok)        
                case KeyPress:
                          fprintf(stdout,"key: %d \n",event.xkey.keycode);
                break;
                //--------------------------------
               
    
                //KEYBOARD KEY(it does not wok)        
                case KeyRelease:
                         fprintf(stdout,"key: %d \n",event.xkey.keycode);
                break;
                //--------------------------------
               
                case ButtonRelease:
                    //show which button is released
                    if (event.xbutton.button==1)
                        fprintf(stdout,"mouse left click");
                    if (event.xbutton.button==2)
                        fprintf(stdout,"middle left click");
                    if (event.xbutton.button==3)
                        fprintf(stdout,"mouse right click");
                break;                
                
            }
        }
    }
    Last edited by hamedhsn; 09-23-2010 at 05:59 PM.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,380
    Please enclose your code in
    Code:
    // This is some code.
    blocks. It will preserve indentations and such, making it a lot easier to read your code.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Sep 2010
    Posts
    14
    Quote Originally Posted by Rubberman View Post
    Please enclose your code in
    Code:
    // This is some code.
    blocks. It will preserve indentations and such, making it a lot easier to read your code.
    Done

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,380
    It is a permissions problem. There are certain events you cannot intercept by default on windows other than your own. This is a security issue and one that should not lightly (or easily) be bypassed. For example, if you could intercept key press/release events, you have the foundation of a keylogger.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •