我知道有一些这样的问题,但很多答案总是有很多但是,如果,你不应该这样做。
我正在尝试做的是有一个后台程序,可以从X11监控键盘事件。这是在一个嵌入式设备上,它将有一个主要的应用程序,基本上运行在类似于kiosk模式的东西。我们希望有一个后台应用程序,管理一些事情,可能是一个后门挂钩。但这个应用程序通常不会有焦点。
我不能使用主应用程序,因为它的一部分是为了在主应用程序失败时的故障安全,或者做一些开发类型的事情来绕过主应用程序。
我发现的最好的问题是几年前的,所以我不确定它是不是最新的。这在windows中是非常容易做到的。
X KeyPress/Release events capturing irrespective of Window in focus
发布于 2014-05-24 03:48:41
正确的方法是使用Xlib。使用这个库,您可以编写如下代码:
while (1) {
XNextEvent(dis, &report);
switch (report.type) {
case KeyPress:
if (XLookupKeysym(&report.xkey, 0) == XK_space) {
fprintf (stdout, "The space bar was pressed.\n");
}
break;
}
}
/*This event loop is rather simple. It only checks for an expose event. XNextEvent waits for an event to occur. You can use other methods to get events, which are documented in the manual page for XNextEvent.*/
/*Now you will learn how to check if an event is a certain key being pressed. The first step is to put case KeyPress: in your switch for report.type. Place it in a similar manner as case Expose.*/您还可以在映射到键盘的特殊设备文件上使用poll或select。在我的例子中是/dev/input/event1。
如果您对映射到您的键盘的特殊文件有疑问,请阅读文件/var/log/Xorg.0.log (搜索单词keyboard)。
这里有另一个您感兴趣的链接:Linux keyboard event capturing /dev/inputX
https://stackoverflow.com/questions/23836859
复制相似问题