[英]Qt - Capturing QKeyEvent from /dev/input/event1 in embedded linux device (EGLFS)
[英]Qt embedded linux event watcher
我在Qt嵌入式應用程序中遇到有關鍵盤輸入讀取的大問題( 如果沒有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庫是否具有允許我執行此類操作的更高級別的機制? 我在尋找一些,但只找到QKeyPress
類。 還是我需要以裸露的C方式進行操作? 我將不勝感激!
編輯:在創建MainWindow
對象之前,我剛剛在Qt應用程序的main
中實現了以上代碼。 該代碼有效,這意味着該應用程序具有讀取輸入的所有必需權限。 Qt為什么不解釋呢?
您需要使用QKeyEvent
類來獲取鍵盤事件。 您可以如下捕獲鍵盤事件
void yourClass :: keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_P)
qDebug() << "Key P is Pressed";
}
如果您根本沒有鍵盤事件,那么請檢查此鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.