您需要在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;
}
你可以找到更多關於input_event結構和代碼的定義從http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/input.h?v=2.6.11.8
例如在我的機器,我意識到,我離開的時候點擊我鼠標發生以下組合
ie.type = 1
ie.value = 1
ie.code = 272
這可能有助於在Linux中捕獲不同的事件。
你想要[KeyLogger](http://en.wikipedia.org/wiki/Keystroke_logging)? –
是的,有點。但我想自己編寫代碼,除非這太複雜了。 –
它比調用一個函數更復雜。在將事件/中斷記錄到後臺應用程序時,您需要在OS或中斷中攔截事件並讓事件或中斷通過。換句話說,這是一個系統級別的功能,而不是應用程序級別。 – lurker