简体   繁体   English

Qt嵌入式Linux事件观察器

[英]Qt embedded linux event watcher

I am facing a big problem regarding keyboard input reading in my Qt embedded application ( Cannot use keyboard within Qt app without sudo ). 我在Qt嵌入式应用程序中遇到有关键盘输入读取的大问题( 如果没有sudo,不能在Qt应用程序中使用键盘 )。 This problem is going for a very long time I dont think it is going to finally be resolved in the normal way. 这个问题将持续很长时间,我认为它不会以正常方式最终得到解决。 Since my Qt app doesnt see the keyboard events (which is /dev/input/event1 ) I think I am forced to watch the device file mysalfe and wait for events to happen and interpret them by hand. 由于我的Qt应用程序看不到键盘事件(即/dev/input/event1 ),我认为我不得不观看设备文件mysalfe,等待事件发生并手动解释它们。

In a raw c++ application I would go for something like this: 在原始的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;
}

I was wondering either Qt library has any higher level mechanisms allowing me to do such thing? 我想知道Qt库是否具有允许我执行此类操作的更高级别的机制? I was looking for some but I have only found QKeyPress class. 我在寻找一些,但只找到QKeyPress类。 Or do I need to do it the bare C way? 还是我需要以裸露的C方式进行操作? I would apreciate all help! 我将不胜感激!

Edit: I have just implemented the above code in the main of my Qt app, before the MainWindow object is created. 编辑:在创建MainWindow对象之前,我刚刚在Qt应用程序的main中实现了以上代码。 The code works, which means the application has all the needed rights to read the input. 该代码有效,这意味着该应用程序具有读取输入的所有必需权限。 Why doesnt Qt interpret it then? Qt为什么不解释呢?

You need to use QKeyEvent class to get keyboard events. 您需要使用QKeyEvent类来获取键盘事件。 You can catch keyboard event as below 您可以如下捕获键盘事件

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

If you are not getting keyboard events at all then you check this link 如果您根本没有键盘事件,那么请检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM