0

I am creating a cross-platform application in Qt5.15 monitoring user activity. My code works fine in Windows,Mac, and Raspberry Pi-Desktop-version(Debian), but when it comes to ubuntu keyboard events and mouse click events are not working. Note that my application is running in the background while the user is working.

#define MOUSEFILE "/dev/input/event4"
#define MOUSEMOVEFILE "/dev/input/js1"
#define KEYBOARDFILE "/dev/input/event0"

Header files included

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/input.h>
#include <linux/input-event-codes.h>
#include <fcntl.h>

For keyboard event code

class KeyboardHook:public QThread
{
    Q_OBJECT
    void run() override
    {
        int fd;
        struct input_event ie;

        if((fd=open(KEYBOARDFILE,O_RDONLY))==-1)
        {
            //qDebug()<<"opening device";
            exit (EXIT_FAILURE);
        }
        while(read(fd,&ie,sizeof(struct input_event)))
        {
           // qDebug()<<"Keyboard Activity";
            if(ie.code==EV_KEY)
            {
                DateAndTime::s_previousActivity=QDateTime::currentDateTime();
                GlobalHook::timeNow.start();
            }
             msleep(500);
        }
    }
};

But mouse-move is working. The code is same only difference KEYBOARDFILE is replaced with MOUSEMOVEFILE and NOT using below if statement

if(ie.code==EV_KEY)

If I don't use above checks for the keyboard I am getting continuous keyboard activity, even if the keyboard is not being touched.

The above code is working correctly in Debian.

when typing

ls -la /dev/input/by-id and ls -la /dev/input/by-path

following i got enter image description here

When typing xinput i am getting enter image description here

My ubantu is installed in virtual-box

Sharmiko
  • 543
  • 4
  • 21
Arun K
  • 413
  • 4
  • 15
  • You need to post a link to a full code set. What version of Ubuntu? 20.04 only has Qt 5.12.8 in repos. The Debian Qt maintainers quit so there will be no future Qt updates in Debian family as of right now. I'm asking because if you built Qt 5.15 from scratch you could have introduced a problem when configuring. I was going to build your stuff on 5.12 under 20.04 LTS to see if it worked, then try it on Manjaro. Manjaro has 5.15.2 and I have a machine already set up. If you are running newer Ubuntu I have to set that up to help you. – user3450148 May 14 '21 at 13:28
  • On my Ubuntu laptop (Focal) `event0` is the lid. What do you see when you type `ls -la /dev/input/by-id`? And `ls -la /dev/input/by-path`? – alle_meije May 17 '21 at 11:52
  • @alle_meije question edited , i tried from event0 to event12 but no luck – Arun K May 17 '21 at 12:14
  • Not sure, maybe the code at https://stackoverflow.com/questions/29678011 lets you find the right number? – alle_meije May 18 '21 at 08:21
  • why would you monitor a user in the background? isn't that something dirty? privacy? – NaturalDemon May 18 '21 at 09:24
  • @ NaturalDemon it is time tracking application not a privacy breaker , user can start and stop this application, Employee(user) will pay only for the hours in which the application is active – Arun K May 18 '21 at 10:21

1 Answers1

1

Your check

if(ie.code==EV_KEY)

should be

if(ie.type==EV_KEY)

The type will tell you the type of event like key-press or mouse movement.

The code will tell you which key was pressed.

Markus Schumann
  • 7,636
  • 1
  • 21
  • 27