我正在尝试使用X11 / Xlib.h在Linux中创建一个简单的全局密钥记录器
我见过Global Hotkey with X11/Xlib,但这似乎是特定于一个热键的。当我尝试致电XGrabKey时,我得到
X Error of failed request: BadAccess (attempt to access private resource denied)
Major opcode of failed request: 33 (X_GrabKey)
这是我当前的代码,对任何按键(或释放)均无反应。
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main() {
Display *display = XOpenDisplay(NULL);
Window root = DefaultRootWindow(display);
XEvent ev;
XSelectInput(display, root, KeyPressMask | KeyReleaseMask );
while(1) {
printf("Waiting\n");
XNextEvent(display, &ev);
switch(ev.type) {
case KeyPress:
printf("Pressed\n");
break;
default:
break;
}
}
XCloseDisplay(display);
return 0;
}