Qtembedded式Linux事件观察

我正在面对一个关于在我的Qtembedded式应用程序的键盘input阅读大问题( 不能使用没有sudo的Qt应用程序内的键盘 )。 这个问题已经很长时间了,我不认为这个问题最终会以正常的方式得到解决。 由于我的Qt应用程序没有看到键盘事件(这是/dev/input/event1 ),我想我不得不观看设备文件mysalfe并等待事件发生并手动解释它们。

在一个原始的C ++应用程序,我会去这样的事情:

 /** BB-BONE-GPIO Test code to test the GPIO-KEYS interface. * Written by Derek Molloy (www.derekmolloy.ie) for the book * Exploring BeagleBone. * * This code is based on work in the document: * www.kernel.org/doc/Documentation/input/input.txt * * Written by Derek Molloy for the book "Exploring BeagleBone: Tools and * Techniques for Building with Embedded Linux" by John Wiley & Sons, 2014 * ISBN 9781118935125. Please see the file README.md in the repository root * directory for copyright and GNU GPLv3 license information. */ #include<iostream> #include<fcntl.h> #include<stdio.h> #include<stdlib.h> #include<linux/input.h> using namespace std; #define KEY_PRESS 1 #define KEY_RELEASE 0 int main(){ int fd, count=0; struct input_event event[64]; if(getuid()!=0){ cout << "You must run this program as root. Exiting." << endl; return -1; } cout << "Starting BB-BONE-GPIO Test (press 10 times to end):" << endl; if ((fd = open("/dev/input/event1", O_RDONLY)) < 0){ perror("Failed to open event1 input device. Exiting."); return -1; } while(count < 20){ // Press and Release are one loop each int numbytes = (int)read(fd, event, sizeof(event)); if (numbytes < (int)sizeof(struct input_event)){ perror("The input read was invalid. Exiting."); return -1; } for (int i=0; i < numbytes/sizeof(struct input_event); i++){ int type = event[i].type; int val = event[i].value; int code = event[i].code; if (type == EV_KEY) { if (val == KEY_PRESS){ cout << "Press : Code "<< code <<" Value "<< val<< endl; } if (val == KEY_RELEASE){ cout << "Release: Code "<< code <<" Value "<< val<< endl; } } } count++; } close(fd); return 0; } 

我想知道Qt库有没有更高级别的机制允许我做这样的事情? 我正在寻找一些,但我只findQKeyPress类。 还是我需要做裸C的方式? 我会apreciate一切帮助!

编辑:我刚才在创buildMainWindow对象之前,在我的Qt应用程序的main中实现了上面的代码。 代码起作用,这意味着应用程序具有读取input所需的所有权限。 为什么Qt不解释呢?

您需要使用QKeyEvent类来获取键盘事件。 您可以捕获键盘事件如下

 void yourClass :: keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_P) qDebug() << "Key P is Pressed"; } 

如果你没有得到键盘事件,那么你检查这个链接