Qt嵌入式linux事件观察程序
Qt embedded linux event watcher
我在Qt嵌入式应用程序中遇到了一个关于键盘输入读取的大问题(如果没有sudo,就无法在Qt应用程序中使用键盘(。这个问题持续了很长一段时间,我认为它最终不会以正常的方式得到解决。由于我的Qt应用程序看不到键盘事件(即/dev/input/event1
(,我想我不得不亲自查看设备文件,等待事件发生并手动解释。
在一个原始的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库是否有更高级别的机制允许我做这样的事情?我在找一些,但我只找到了QKeyPress
类。还是我需要用C的方式来做?我会感谢所有的帮助!
编辑:在创建MainWindow
对象之前,我刚刚在Qt应用程序的main
中实现了上述代码。代码有效,这意味着应用程序具有读取输入所需的所有权限。那Qt为什么不解释呢?
您需要使用QKeyEvent
类来获取键盘事件。你可以捕捉键盘事件如下
void yourClass :: keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_P)
qDebug() << "Key P is Pressed";
}
如果你根本没有得到键盘事件,那么你可以检查这个链接
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 跨平台C++文件系统观察程序
- 编译器降低程序的时间复杂性合法吗?这被认为是可观察的行为吗
- 如何在c++程序中观察gdb中的变量表
- 程序状态的任何变化是否构成可观察的行为?
- Qt嵌入式linux事件观察程序