简体   繁体   中英

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 ). 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.

In a raw c++ application I would go for something like this:

/** 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? I was looking for some but I have only found QKeyPress class. Or do I need to do it the bare C way? 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. The code works, which means the application has all the needed rights to read the input. Why doesnt Qt interpret it then?

You need to use QKeyEvent class to get keyboard events. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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