Grab the key in X11

来源:互联网 发布:生石花种子淘宝推荐 编辑:程序博客网 时间:2024/05/22 13:30

本文是搜集网上的资料整理而成的。

方法1:

#include <X11/Xlib.h>#include <stdio.h>main(){  Window root;  XEvent e;  int F2,F3,F4,F5,F6;     // Open the display  Display *dpy = XOpenDisplay(0);  if (!dpy)return;  // Get the root window -- so keys will be global  root = DefaultRootWindow(dpy);     F2 = XKeysymToKeycode(dpy, XStringToKeysym("F2"));  F3 = XKeysymToKeycode(dpy, XStringToKeysym("F3"));  F4 = XKeysymToKeycode(dpy, XStringToKeysym("F4"));  F5 = XKeysymToKeycode(dpy, XStringToKeysym("F5"));  F6 = XKeysymToKeycode(dpy, XStringToKeysym("F6"));     // register the keys  XGrabKey(dpy, F2, 0, root, True, GrabModeAsync, GrabModeAsync);  XGrabKey(dpy, F3, 0, root, True, GrabModeAsync, GrabModeAsync);  XGrabKey(dpy, F4, 0, root, True, GrabModeAsync, GrabModeAsync);  XGrabKey(dpy, F5, 0, root, True, GrabModeAsync, GrabModeAsync);  XGrabKey(dpy, F6, 0, root, True, GrabModeAsync, GrabModeAsync);     // wait for events  for(;;) {  XNextEvent(dpy, &e);  if (e.type == KeyPress){  if (e.xkey.keycode == F4)  break;  else{  if (e.xkey.keycode == F2)printf("F2!!!\n");  else if (e.xkey.keycode == F3)printf("F3!!!\n");  else if (e.xkey.keycode == F5)printf("F5!!!\n");  else if (e.xkey.keycode == F6)printf("F6!!!\n");  }  }  }  // clean up  XUngrabKey(dpy, F2, 0, root);  XUngrabKey(dpy, F3, 0, root);  XUngrabKey(dpy, F4, 0, root);  XUngrabKey(dpy, F5, 0, root);  XUngrabKey(dpy, F6, 0, root);}

方法2:

#include <X11/Xlib.h>#include <X11/extensions/record.h>#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <glib-2.0/glib.h>static gboolean stop_thread = FALSE;static Display* dpy = 0;static XRecordContext rc;static pthread_t thread;GMainLoop *main_loop;void* intercept_key_thread(void* data);void key_pressed_cb(XPointer arg, XRecordInterceptData *d);int main(int argc, char *argv[]){pthread_create(&thread, NULL, intercept_key_thread, NULL);main_loop = g_main_loop_new (NULL, FALSE);     g_main_loop_run (main_loop);  g_main_loop_unref (main_loop);  return 0;}void* intercept_key_thread(void* data) {  XRecordClientSpec rcs;  XRecordRange* rr;  dpy = XOpenDisplay(0);  if (!(rr = XRecordAllocRange())) {    fprintf(stderr, "XRecordAllocRange error\n");    exit(1);  }  rr->device_events.first = KeyPress;  rr->device_events.last = MotionNotify;  rcs = XRecordAllClients;  if (!(rc = XRecordCreateContext(dpy, 0, &rcs, 1, &rr, 1))) {    fprintf(stderr, "XRecordCreateContext error\n");    exit(1);  }  XFree(rr);  if (!XRecordEnableContext(dpy, rc, key_pressed_cb, data)) {    fprintf(stderr, "XRecordEnableContext error\n");    exit(1);  }  return 0;}void stop_intercept_key_thread(void) {  void* result;  stop_thread = TRUE;  if (0 != pthread_join(thread, &result)) {    fprintf(stderr, "pthread_join error\n");    exit(1);  }}void key_pressed_cb(XPointer arg, XRecordInterceptData *d) {  if (stop_thread) {    if (!XRecordDisableContext(dpy, rc)) {      fprintf(stderr, "XRecordDisableContext error\n");      exit(1);    }    if (!XRecordFreeContext(dpy, rc)) {      fprintf(stderr, "XRecordFreeContext error\n");      exit(1);    }    pthread_exit(0);  }  if (d->category != XRecordFromServer) return;  unsigned char type = ((unsigned char*)d->data)[0] & 0x7F;  unsigned char keycode = ((unsigned char*)d->data)[1];  if (type == KeyPress) {printf("key: %d\n", keycode);  }}

编译方法:

gcc file.c  `pkg-config --cflags --libs  glib-2.0` -lpthread -lX11 -lXtst

方法3:

/** * Author:      jouyouyun <jouyouwen717@gmail.com> * Maintainer:  jouyouyun <jouyouwen717@gmail.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see <http://www.gnu.org/licenses/>. **/ #include <glib.h>#include <X11/Xlib.h>#include <X11/extensions/record.h> static void event_cb (XPointer user_data, XRecordInterceptData *hook); int main (int argc, char *argv[]){    Display *ctrl_disp = XOpenDisplay(NULL);    Display *data_disp = XOpenDisplay(NULL);     if ( !ctrl_disp || !data_disp ) {        g_warning ("Unable to connect to X11 display!");        return -1;    }     gint major, minor, dummy;     if ( !XQueryExtension (ctrl_disp, "XTEST", &major, &minor, &dummy) ) {        g_warning ("XTest extension missing!");        return -1;    }     if ( !XRecordQueryVersion (ctrl_disp, &major, &minor) ) {        g_warning ("Failed to obtain xrecord version!");        return -1;    }     XSynchronize(ctrl_disp, True);    XFlush (ctrl_disp);     XRecordRange *range = XRecordAllocRange ();    /*     * 设定事件监听的范围     * 监听 first ~ last 之间的事件     */    range->device_events.first = KeyPress;    range->device_events.last = ButtonRelease;     XRecordClientSpec spec = XRecordAllClients;    XRecordContext context = XRecordCreateContext (                                 data_disp, 0, &spec, 1, &range, 1 );     if ( !context ) {        g_warning ("Failed to create context!");        return -1;    }     if ( !XRecordEnableContext(data_disp, context, event_cb, NULL) ) {        g_warning ("Failed to enable context!");        return -1;    }     /*    if ( !XRecordEnableContextAsync (data_disp, context, event_cb, NULL) ) {        g_warning ("Failed to enable context!");        return -1;    }     XRecordProcessReplies (data_disp);    */     XRecordDisableContext(data_disp, context);    XRecordFreeContext (data_disp, context);    XFree(range);     XCloseDisplay (data_disp);    XCloseDisplay (ctrl_disp);    return 0;} static voidevent_cb (XPointer user_data, XRecordInterceptData *hook){    if ( hook->category != XRecordFromServer ) {        g_warning ("Data not from server!");        return;    }     int event_type = hook->data[0];    KeyCode keycode = hook->data[1];    g_print ("Intercepted event type: %d, code: %d\n",             event_type, keycode);     switch (event_type) {        case KeyPress:            g_print ("key press event!\n");            break;         case KeyRelease:            g_print ("key release event!\n");            break;         case ButtonPress:            g_print ("button press event!\n");            break;         case ButtonRelease:            g_print ("button release event!\n");            break;         default:            /* FIXME: other events grab */            break;    }}


References:

1. https://github.com/Aitjcize/Qwertickle/blob/master/src/qwertickle.c

2. http://incise.org/tinywm.html

3. http://jouyouyun.is-programmer.com/posts/42193.html

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 肩膀很疼怎么办睡不着觉 夏天穿内衣出汗后很臭怎么办 脸过敏后严重缺水怎么办 过敏后脸上反复出现湿疹怎么办 孕妇脸上长湿疹过敏红肿怎么办 一岁宝宝一直流鼻涕怎么办 三岁宝宝一直流鼻涕怎么办 3岁宝宝一直流鼻涕不好怎么办 7岁半边鼻子不通气怎么办 感冒流鼻涕鼻子不通气怎么办 鼻子不通气干的怎么办 7个月婴儿流鼻涕怎么办 鼻子火辣辣的光想流鼻涕怎么办? 三岁宝宝鼻塞流鼻涕怎么办 一岁宝宝感冒流鼻涕鼻塞怎么办 1岁宝宝鼻塞流鼻涕怎么办 一岁多宝宝感冒鼻塞流鼻涕怎么办 宝宝鼻塞流鼻涕怎么办速效办法 八个月宝宝感冒流鼻涕鼻塞怎么办 7岁儿童流清鼻涕怎么办 1岁的宝宝流鼻涕怎么办 3岁宝宝鼻塞严重怎么办 宝宝流鼻涕2个月怎么办 一个月宝宝流鼻子怎么办 六个月宝宝流清水鼻涕怎么办 婴儿流鼻涕怎么办最简单方法 婴儿咳嗽流鼻涕怎么办最简单方法 6个月小孩流鼻涕怎么办 宝宝风寒感冒咳嗽流鼻涕怎么办 三个月宝宝流清鼻涕怎么办 三个月宝宝留清鼻涕怎么办 三个月婴儿流清鼻涕怎么办 宝宝7个月流鼻涕怎么办 6个月孩子流鼻涕怎么办 十一个月孩子感冒流鼻涕怎么办 7个月孩子流鼻涕怎么办 18个月的宝宝流鼻涕怎么办 6个月宝宝咳嗽流鼻涕怎么办 18个月宝宝咳嗽流鼻涕怎么办 6个月宝宝流鼻涕打喷嚏怎么办 宝宝18个月流鼻涕怎么办