![](/img/trans.png)
[英]Catching output of linux command in background C++ program / bash script
[英]Background program in C++ for Linux
我不確定應該使用哪些關鍵字來搜索此關鍵字,因此我將在這里詢問。 很抱歉,如果重復的話。
基本上,我想執行以下操作
./my_prog &
其中my_prog是用C ++ 14編碼的,
(這樣一來,我每天結束時就能查看我執行上述任何一項操作的頻率。)
首先,我想使用Qt,但不久之后我意識到Qt僅在自己的窗口中執行此操作。 (或者至少,這是我所能使用的。)這無濟於事,因為我希望讓my_prog
對每次單擊和按鍵計數。
有人知道我應該使用哪些庫/函數嗎? 謝謝。
您需要在Linux中閱讀鼠標設備。 在我的Ubuntu中,該設備為“ / dev / input / event4”,您可以從“ / proc / bus / input / devices”檢查您的設備。
在linux / input.h標頭中,您可以找到可用於處理不同鼠標事件的'input_event'結構。
這是簡單的例子
#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;
}
您可以從http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/input.h?v=2.6.11.8中找到有關input_event結構和代碼定義的更多信息。
例如,在我的機器上,我意識到當我左鍵單擊鼠標時,會發生以下組合
ie.type = 1
ie.value = 1
ie.code = 272
這有助於捕獲Linux中的不同事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.