简体   繁体   English

适用于Linux的C ++中的后台程序

[英]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, 其中my_prog是用C ++ 14编码的,

  • adds a character to file A whenever I right click. 每当我右键单击时,都会向文件A添加一个字符。
  • adds a character to file B whenever I left click. 每当我单击鼠标左键时,都会在文件B中添加一个字符。
  • adds a character to file C whenever I press a key. 每当我按下一个键时,都会在文件C中添加一个字符。

(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. 首先,我想使用Qt,但不久之后我意识到Qt仅在自己的窗口中执行此操作。 (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. (或者至少,这是我所能使用的。)这无济于事,因为我希望让my_prog对每次单击和按键计数。

Anyone know what library/functions I should use? 有人知道我应该使用哪些库/函数吗? Thanks. 谢谢。

You need to read your mouse device in Linux. 您需要在Linux中阅读鼠标设备。 In my Ubuntu that device is '/dev/input/event4', you can check yours from '/proc/bus/input/devices'. 在我的Ubuntu中,该设备为“ / dev / input / event4”,您可以从“ / proc / bus / input / devices”检查您的设备。

In linux/input.h header you can find 'input_event' struct which can be used to handle different mouse events. 在linux / input.h标头中,您可以找到可用于处理不同鼠标事件的'input_event'结构。

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 您可以从http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/input.h?v=2.6.11.8中找到有关input_event结构和代码定义的更多信息

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. 这有助于捕获Linux中的不同事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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