0
我在Qt嵌入式應用程序(Cannot use keyboard within Qt app without sudo)中遇到了有關鍵盤輸入讀取的大問題。這個問題持續了很長一段時間,我不認爲它會以正常的方式最終得到解決。由於我的Qt應用程序沒有看到鍵盤事件(這是/dev/input/event1
),我想我不得不觀看設備文件mysalfe並等待事件發生並手動解釋它們。Qt嵌入式Linux事件觀察器
在原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的方式來做到這一點?我會apreciate所有幫助!
編輯: 在創建MainWindow
對象之前,我剛纔在我的Qt應用程序的main
中實現了上述代碼。代碼起作用,這意味着應用程序具有讀取輸入所需的所有權限。爲什麼QT不解釋它呢?
我已經看到了這方面我覺得這個環節,每個環節等在互聯網上......從字面上...它不適用於我的情況,這就是爲什麼我想手動觀看設備。我的應用程序不會停止與sigfault,它只是沒有看到按鍵。 – Bremen
在您的beaglebone終端類型'sudo nano/etc/environment'中,然後添加'QWS_KEYBOARD'行並重新啓動 –
我有這個,不起作用。 – Bremen