监听键盘事件而不消耗X11 – 键盘挂钩

我尝试编写一个程序,在Ubuntu(KDE)中按下键盘消息来发送每个键的名称; 而不会干扰程序中正常的键盘操作(只是通知键名)。

这是我的程序:

#include <X11/Xlib.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> using namespace std; void SendPressKeyEvent(Display *display, XKeyEvent xkey) { Window current_focus_window; int current_focus_revert; XGetInputFocus(display, &current_focus_window, &current_focus_revert); xkey.type = KeyPress; xkey.display = display; xkey.window = current_focus_window; xkey.root = DefaultRootWindow(display); xkey.subwindow = None; xkey.time = 1000 * time(0); xkey.x = 0; xkey.y = 0; xkey.x_root = 0; xkey.y_root = 0; xkey.same_screen = True; XSendEvent(display, InputFocus, True, KeyPressMask, (XEvent *)(&xkey)); } void SendReleaseKeyEvent(Display *display, XKeyEvent xkey) { Window current_focus_window; int current_focus_revert; XGetInputFocus(display, &current_focus_window, &current_focus_revert); xkey.type = KeyRelease; xkey.display = display; xkey.window = current_focus_window; xkey.root = DefaultRootWindow(display); xkey.subwindow = None; xkey.time = 1000 * time(0); xkey.x = 0; xkey.y = 0; xkey.x_root = 0; xkey.y_root = 0; xkey.same_screen = True; XSendEvent(display, InputFocus, True, KeyReleaseMask, (XEvent *)(&xkey)); } void *TaskCode(void* arg) { switch(*(int*)arg) { case 38: system("espeak -v en " "\"a\""); } return 0; } int main() { Display *display = XOpenDisplay(0); if(display == 0) exit(1); XGrabKeyboard(display, DefaultRootWindow(display), True, GrabModeAsync, GrabModeAsync, CurrentTime); XEvent event; while(true) { XNextEvent(display, &event); if(event.type == Expose) { } if(event.type == KeyPress) { SendPressKeyEvent(display,event.xkey); if(event.xkey.keycode == 38) { pthread_t thread; int thread_arg = event.xkey.keycode; pthread_create(&thread,0, TaskCode, (void*) &thread_arg); } } if(event.type == KeyRelease) SendReleaseKeyEvent(display,event.xkey); } XCloseDisplay(display); } 

这个程序只是关键的一个可以扩展到其他键。

但是当这个程序正在运行时,一些程序(例如Chromium)不会在其编辑框中显示闪烁(光标)。 所有的KDE热键都会被禁用。

这怎么解决?

Solutions Collecting From Web of "监听键盘事件而不消耗X11 – 键盘挂钩"

这是我的快速和肮脏的例子

 #include <X11/Xh> #include <X11/Xlib.h> #include <X11/Xutil.h> #include <stdio.h> #include <ctype.h> int main () { Display* d = XOpenDisplay(NULL); Window root = DefaultRootWindow(d); Window curFocus; char buf[17]; KeySym ks; XComposeStatus comp; int len; int revert; XGetInputFocus (d, &curFocus, &revert); XSelectInput(d, curFocus, KeyPressMask|KeyReleaseMask|FocusChangeMask); while (1) { XEvent ev; XNextEvent(d, &ev); switch (ev.type) { case FocusOut: printf ("Focus changed!\n"); printf ("Old focus is %d\n", (int)curFocus); if (curFocus != root) XSelectInput(d, curFocus, 0); XGetInputFocus (d, &curFocus, &revert); printf ("New focus is %d\n", (int)curFocus); if (curFocus == PointerRoot) curFocus = root; XSelectInput(d, curFocus, KeyPressMask|KeyReleaseMask|FocusChangeMask); break; case KeyPress: printf ("Got key!\n"); len = XLookupString(&ev.xkey, buf, 16, &ks, &comp); if (len > 0 && isprint(buf[0])) { buf[len]=0; printf("String is: %s\n", buf); } else { printf ("Key is: %d\n", (int)ks); } } } } 

这是不可靠的,但大部分时间工作。 (正在显示我正在键入此框中的键)。 您可能会调查为什么有时会失败;)原则上也不能显示热键。 热键是一把钥匙,只有一个客户端可以拿到一把钥匙。 在这里完全没有任何东西可以完成,只是加载一个专门为此目的而设计的特殊X11扩展(例如XEvIE)。

感谢nm的回答和parsa的评论,这是我最后的代码:

 #include <X11/Xlib.h> #include <stdlib.h> #include <iostream> using namespace std; void* TaskCode(void* parg) { int keycode = *((int*)parg); cout<< "\n\n" << keycode << "\n\n"; if(keycode == XKeysymToKeycode(XOpenDisplay(0),'a')) system("espeak -v en " "\"a\""); delete (int*)parg; return 0; } void Action(int keycode) { pthread_t thread; pthread_attr_t attrs; pthread_attr_init(&attrs); pthread_attr_setdetachstate(&attrs,PTHREAD_CREATE_DETACHED); pthread_attr_setstacksize(&attrs, 1000); int* pthread_arg = new int; *pthread_arg = keycode; pthread_create(&thread,&attrs, TaskCode, (void*) pthread_arg); } int MyX11ErrorHandler(Display *, XErrorEvent *error_event) { cout << "\n\n" "An X11-Functions error occured. Probably the focused window was closed.""\n" "This error will be ignored." "\n"; cout<< "error_code: " << (unsigned)error_event -> error_code << "\n"; cout<< "minor_code: " << (unsigned)error_event -> minor_code << "\n"; cout<< "request_code: " << (unsigned)error_event -> request_code << "\n"; cout<< "resourceid: " << error_event -> resourceid << "\n"; cout<< "serial; " << error_event -> serial << "\n"; cout<< "type: " << error_event -> type << "\n\n"; return 0; } int main() { Display* display = XOpenDisplay(0); Window root = DefaultRootWindow(display); Window current_focus_window; int revert; XSetErrorHandler(MyX11ErrorHandler); XGetInputFocus(display, &current_focus_window, &revert); XSelectInput(display,current_focus_window,KeyPressMask | KeyReleaseMask | FocusChangeMask); while(true) { XEvent event; XNextEvent(display, &event); switch (event.type) { case FocusOut: if(current_focus_window != root) XSelectInput(display, current_focus_window, 0); XGetInputFocus(display, &current_focus_window, &revert); if(current_focus_window == PointerRoot) current_focus_window = root; XSelectInput(display, current_focus_window, KeyPressMask|KeyReleaseMask|FocusChangeMask); break; case KeyPress: Action(event.xkey.keycode); break; } } } 

将这些添加到Qt Creator的项目.pro文件中:

 LIBS += -lX11 LIBS += -lpthread LIBS += -lXtst 

任何改进建议表示赞赏。

要归档我也加上我最后的代码抓住:

 #include <X11/Xlib.h> #include <X11/Xutil.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> using namespace std; void* TaskCode(void* parg) { int keycode = *((int*)parg); cout<< "\n\n" << keycode << "\n\n"; system("espeak -v en " "\"a\""); delete (int*)parg; return 0; } void SendKeyEvent(Display *display, XEvent event) { Window current_focus_window; XKeyEvent& xkey = event.xkey; int current_focus_revert; XGetInputFocus(display, &current_focus_window, &current_focus_revert); xkey.state = Mod2Mask; XSendEvent(display, InputFocus, True, xkey.type, (XEvent *)(&event)); } int GrabKey(Display* display, Window grab_window, int keycode) { unsigned int modifiers = Mod2Mask; // numlock on //Window grab_window = DefaultRootWindow(display); Bool owner_events = True; int pointer_mode = GrabModeAsync; int keyboard_mode = GrabModeAsync; XGrabKey(display, keycode, modifiers, grab_window, owner_events, pointer_mode, keyboard_mode); return keycode; } void UngrabKey(Display* display, Window grab_window, int keycode) { unsigned int modifiers = Mod2Mask; // numlock on // Window grab_window = DefaultRootWindow(display); XUngrabKey(display,keycode,modifiers,grab_window); } void Action(int keycode) { pthread_t thread; int* pthread_arg = new int; *pthread_arg = keycode; pthread_create(&thread,0, TaskCode, (void*) pthread_arg); } int main() { Display* display = XOpenDisplay(0); Window root = DefaultRootWindow(display); XEvent event; int keycode = XKeysymToKeycode(display,'a'); GrabKey(display,root,keycode); XSelectInput(display, root, KeyPressMask | KeyReleaseMask); while(true) { XNextEvent(display, &event); switch(event.type) { case KeyPress: Action(event.xkey.keycode); case KeyRelease: SendKeyEvent(display,event); default: break; } } XCloseDisplay(display); } 

除了与有问题的代码不同,它忽略了语言布局。 按一种类型a任何regregless的语言布局!

作为侦听X事件的替代方法,也可以直接监听Linux输入事件: https : //stackoverflow.com/a/27693340/21501

这有一个好处,就是可以修改事件流,并拦截,编辑或生成输入事件。

我知道这是为时已晚,但我认为它相关…从Eugene的键盘记录源代码

源代码:XLib键盘记录器

https://gist.github.com/enizhibitsky/846474