gpt4 book ai didi

c++ - Qt嵌入式Linux事件观察器

转载 作者:太空宇宙 更新时间:2023-11-04 03:37:04 24 4
gpt4 key购买 nike

我在 Qt 嵌入式应用程序 ( Cannot use keyboard within Qt app without sudo ) 中面临有关键盘输入读取的大问题。这个问题已经持续了很长时间,我认为它最终不会以正常方式得到解决。由于我的 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 库是否有任何更高级别的机制允许我做这样的事情?我一直在寻找一些,但我只找到了 QKeyPress 类。或者我需要用纯粹的 C 方式来做吗?我将感谢所有帮助!

编辑:在创建 MainWindow 对象之前,我刚刚在 Qt 应用程序的 main 中实现了上述代码。该代码有效,这意味着应用程序拥有读取输入所需的所有权限。为什么 Qt 不解释它呢?

最佳答案

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

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

如果您根本没有收到键盘事件,那么您检查 this link

关于c++ - Qt嵌入式Linux事件观察器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31191149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com