0

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?

Community
  • 1
  • 1
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74

1 Answers1

0

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

Community
  • 1
  • 1
Amol Saindane
  • 1,568
  • 10
  • 19
  • I have seen this link and every other link on the internet regarding this i think... Literally... It doesnt work in my case, thats why I want to watch the device manually. My app doesnt stop with sigfault, it just doesnt see the key presses. – Łukasz Przeniosło Jul 02 '15 at 17:58
  • in your beaglebone terminal type `sudo nano /etc/environment` then add `QWS_KEYBOARD` lines and reboot – Amol Saindane Jul 02 '15 at 18:01
  • Also, something that makes me wonder- When I run the app directly with sudo and the keyboard works, the attached usb keyboard works as well (one keyboard is usb and another one is `event1`), no matter what QWS_KEYBOARD is. – Łukasz Przeniosło Jul 02 '15 at 18:08
  • why dont you run your code as `sudo` always ? any specific reason ? – Amol Saindane Jul 02 '15 at 18:17
  • I wish that worked. When I run with sudo via ssh- it doesnt work. If i run a startup script in `rc.local` with sudo- it doesnt work as well, and thats the most important case, app should start at system boot. This is driving me nuts. It only works if i run it with sudo after i manually log in and run in on the attached keyboard and lcd screen. And btw, I am root in every case. – Łukasz Przeniosło Jul 02 '15 at 18:27
  • So, do you know maybe a way using Qt libs to watch the file in an efficient way? – Łukasz Przeniosło Jul 02 '15 at 18:34
  • @ŁukaszPrzeniosło : I tried on Beagle Bone directly. Type `sudo nano /etc/environment` then add `QWS_KEYBOARD=""`, then keyboard works, using `Ctrl+C`, closes the App – Amol Saindane Jul 03 '15 at 04:54
  • But does it work when you type that via ssh and through startup script in rc.local? Thats the problem. Also can u type in anytging or only kill the program with ctrl c? – Łukasz Przeniosło Jul 03 '15 at 04:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82255/discussion-between-amol-and-lukasz-przenioslo). – Amol Saindane Jul 03 '15 at 05:09