Advertisement
bremenpl

keyboard test

Jul 3rd, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include <QApplication>
  3.  
  4. // standard libs
  5. #include <unistd.h>
  6. #include <iostream>
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <linux/input.h>
  11. using namespace std;
  12.  
  13. #define KEY_PRESS 1
  14. #define KEY_RELEASE 0
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.     /*if(getuid() != 0)
  19.     {
  20.         std::cout << "Not logged as root: " << getenv("USER") << " quiting" << std::endl;
  21.         return -1;
  22.     }*/
  23.  
  24.     int fd, count=0;
  25.        struct input_event event[64];
  26.        if(getuid()!=0){
  27.           cout << "You must run this program as root. Exiting." << endl;
  28.           return -1;
  29.        }
  30.        cout << "Starting BB-BONE-GPIO Test (press 10 times to end):" << endl;
  31.        if ((fd = open("/dev/input/event1", O_RDONLY)) < 0){
  32.           perror("Failed to open event1 input device. Exiting.");
  33.           return -1;
  34.        }
  35.        while(count < 20){  // Press and Release are one loop each
  36.           int numbytes = (int)read(fd, event, sizeof(event));
  37.           if (numbytes < (int)sizeof(struct input_event)){
  38.              perror("The input read was invalid. Exiting.");
  39.              return -1;
  40.           }
  41.           for (int i=0; i < numbytes/sizeof(struct input_event); i++){
  42.              int type = event[i].type;
  43.              int val  = event[i].value;
  44.              int code = event[i].code;
  45.              if (type == EV_KEY) {
  46.                 if (val == KEY_PRESS){
  47.                    cout << "Press  : Code "<< code <<" Value "<< val<< endl;
  48.                 }
  49.                 if (val == KEY_RELEASE){
  50.                    cout << "Release: Code "<< code <<" Value "<< val<< endl;
  51.                 }
  52.              }
  53.           }
  54.           count++;
  55.        }
  56.        close(fd);
  57.  
  58.     QApplication a(argc, argv);
  59.     MainWindow w;
  60.     w.show();
  61.  
  62.     return a.exec();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement