简体   繁体   中英

Background program in C++ for Linux

I'm not sure as to what keywords I should use to search this, so I'm going to ask here. I'm sorry if that's a duplicate.

Basically, I'd like to do the following

./my_prog &

where my_prog, coded in C++14,

  • adds a character to file A whenever I right click.
  • adds a character to file B whenever I left click.
  • adds a character to file C whenever I press a key.

(That would enable me to see how often I do any of the above at the end of the day.)

First I wanted to use Qt but I realized soon afterwards that Qt does that in its own window only. (Or at least, that's as far as I can use it.) That wouldn't help as I'd rather have my_prog count every single click and key-press.

Anyone know what library/functions I should use? Thanks.

You need to read your mouse device in Linux. In my Ubuntu that device is '/dev/input/event4', you can check yours from '/proc/bus/input/devices'.

In linux/input.h header you can find 'input_event' struct which can be used to handle different mouse events.

Here is simple example

#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <linux/input.h>

#define MOUSEFILE "/dev/input/event4"

int main()
{
  int fd;
  struct input_event ie;

 if((fd = open(MOUSEFILE, O_RDONLY)) == -1) {
    perror("Cannot access mouse device");
     exit(EXIT_FAILURE);
      }
 while(read(fd, &ie, sizeof(struct input_event))) {
   printf("%d, %d, %d\n", ie.type, ie.value, ie.code);
 }
 return 0;

}

You can find out more about input_event struct and code definitions from http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/input.h?v=2.6.11.8

For example in my machine I realized that when I left click my mouse the following combination occurs

ie.type = 1
ie.value = 1
ie.code = 272

This can be helpful to catch different events in Linux.

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