Why XGrabKey generates extra focus-out and focus-in events?

后端 未结 7 2082
时光说笑
时光说笑 2020-12-14 00:33

Does anyone know an xlib function to trap a keypress event without losing the original focus? How to get rid of it?

(or \"to use XGrabKey() without generating Grab-s

7条回答
  •  隐瞒了意图╮
    2020-12-14 01:17

    Looks like XQueryKeymap will sort you. See below for C++ source code I found:

    /* compile with g++ keytest.cpp -LX11 -o keytest */
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    double gettime() {
     timeval tim;
     gettimeofday(&tim, NULL);
     double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
     return t1;
    }
    
    int main() {
     Display *display_name;
     int depth,screen,connection;
     display_name = XOpenDisplay(NULL);
     screen = DefaultScreen(display_name);
     depth = DefaultDepth(display_name,screen);
     connection = ConnectionNumber(display_name);
     printf("Keylogger started\n\nInfo about X11 connection:\n");
     printf(" The display is::%s\n",XDisplayName((char*)display_name));
     printf(" Width::%d\tHeight::%d\n",
     DisplayWidth(display_name,screen),
     DisplayHeight(display_name,screen));
     printf(" Connection number is %d\n",connection);
    
     if(depth == 1)
      printf(" You live in prehistoric times\n");
     else
      printf(" You've got a coloured monitor with depth of %d\n",depth);
    
     printf("\n\nLogging started.\n\n");
    
     char keys_return[32];
     while(1) {
      XQueryKeymap(display_name,keys_return);
      for (int i=0; i<32; i++) {
       if (keys_return[i] != 0) {
        int pos = 0;
        int num = keys_return[i];
        printf("%.20f: ",gettime());
        while (pos < 8) {
         if ((num & 0x01) == 1) {
          printf("%d ",i*8+pos);
         }
         pos++; num /= 2;
        }
        printf("\n");
       }
      }
      usleep(30000);
     }
     XCloseDisplay(display_name);
    }
    

    Note, this isn't tested code, nor is it mine -- I merely found it on the Internet.

提交回复
热议问题