【问题标题】:Qt embedded linux event watcherQt 嵌入式 linux 事件观察器
【发布时间】:2015-09-20 08:54:28
【问题描述】:

在我的 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 不解释呢?

【问题讨论】:

    标签: c++ linux qt events keyboard


    【解决方案1】:

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

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

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

    【讨论】:

    • 我已经看过这个链接和互联网上的所有其他链接,我认为......从字面上看......它在我的情况下不起作用,这就是我想手动观看设备的原因。我的应用程序不会因 sigfault 而停止,它只是看不到按键。
    • 在您的 beaglebone 终端中输入 sudo nano /etc/environment 然后添加 QWS_KEYBOARD 行并重新启动
    • 另外,让我好奇的是——当我直接使用 sudo 运行应用程序并且键盘工作时,连接的 USB 键盘也能工作(一个键盘是 USB,另一个是 event1),不管 QWS_KEYBOARD 是什么。
    • 为什么不总是以sudo 的身份运行代码?有什么具体原因吗?
    • 我希望这能奏效。当我通过 ssh 使用 sudo 运行时,它不起作用。如果我使用 sudo 在rc.local 中运行启动脚本,它就不能正常工作,这是最重要的情况,应用程序应该在系统启动时启动。这让我发疯了。只有在我手动登录并在附加的键盘和液晶屏上运行后使用 sudo 运行它时它才有效。顺便说一句,我在任何情况下都是根。
    猜你喜欢
    • 2019-03-31
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多