【发布时间】:2014-03-06 10:44:15
【问题描述】:
我正在尝试使用 XLib 捕获按键事件。但由于某些原因 XNextEvent 无法正常工作。 我没有收到任何错误,但看起来我的程序卡在“XNextEvent”调用行上。 这是我的代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
using namespace std;
int main()
{
XEvent event;
KeySym key;
char text[255];
Display *dis;
dis = XOpenDisplay(NULL);
while (1) {
XNextEvent(dis, &event);
if (event.type==KeyPress && XLookupString(&event.xkey,text,255,&key,0) == 1) {
if (text[0]=='q') {
XCloseDisplay(dis);
return 0;
}
printf("You pressed the %c key!\n", text[0]);
}
}
return 0;
}
【问题讨论】:
-
“不起作用”从不是一个好的诊断。发生了什么 ?有错误信息吗?还是意想不到的结果?请编辑您的问题,以准确说明它是如何不起作用的。
-
@hivert 很明显问题出在哪里。无需编辑
-
也许吧,但我认为向 SO 新手解释如何提出好问题很重要。