我知道这其中有一些,但是很多答案总是有很多但是,如果,你不应该这样做。
我要做的是有一个后台程序,可以从X11监视键盘事件。这是在一个嵌入式设备上,它将有一个主应用程序基本上运行在类似于一个信息亭模式。我们希望有一个后台应用程序来管理一些事情,可能还有一个后门挂钩。但这个应用程序通常不会有焦点。
我不能使用主应用程序,因为如果主应用程序出现故障,它的一部分用于故障保护,或者做一些开发类型的事情来绕过主应用程序。
我发现最好的问题是几岁了,所以我不确定它有多新。这在窗户上做起来非常容易。
X KeyPress/Release events capturing irrespective of Window in focus

最佳答案

正确的方法是使用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.*/

也可以在映射到键盘的特殊设备文件上使用pollselect。我的情况是/dev/input/event1
如果您对映射到keyborad的特殊文件有疑问,请阅读文件/var/log/Xorg.0.log(搜索单词keyboard)。
这里有另一个感兴趣的链接:Linux keyboard event capturing /dev/inputX

10-01 06:19
查看更多