[英]How to control mouse movement in linux?
我嘗試在Linux中控制鼠標。 Xlib似乎有效,但是當我嘗試將它與OpenCV一起使用時,它會一直返回:
Resource temporarily unavailable
所以我決定寫“/ dev / psaux”。 代碼如下:
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
unsigned char a[5]={0, 0xff, 0, 0x28, 0xff};
int fp = open ("/dev/psaux", O_WRONLY);
if(!fp)printf("open error:%s\n", strerror(errno));
for(int i = 0; i < 10; i++)
printf("write:%d\t\t%s\n", write(fp, a, 5), strerror(errno));
close(fp);
return 0;
}
編譯它:
gcc my_psaux.c -o my_psaux -std=gnu99 -g
跑,得到
$sudo ./my_psaux
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
write:5 Success
但是鼠標不動。 然后我打開一個新終端,鍵入“sudo cat / dev / psaux”並運行“my_psaux”。 但我什么都沒有。 什么都寫入“/ dev / psaux”?
誰能幫助我?
如果這不是控制鼠標的好方法,有人能告訴我另一個嗎?
非常感謝@R ..提醒我一些其他方式而不是/dev/psaux
所以我嘗試了/dev/input/mouse*
和/dev/input/event*
通過使用
cat /proc/bus/input/devices
我明白了:
I: Bus=0003 Vendor=0461 Product=4d81 Version=0111
N: Name="USB Optical Mouse"
P: Phys=usb-0000:00:1d.0-1/input0
S: Sysfs=/devices/pci0000:00/0000:00:1d.0/usb6/6-1/6-1:1.0/input/input10
U: Uniq=
H: Handlers=mouse2 event10
B: EV=17
B: KEY=70000 0 0 0 0 0 0 0 0
B: REL=143
B: MSC=10
測試后,只有/dev/input/event10
有效。 代碼如下:
#include <stdio.h>
#include <unistd.h>
#include <linux/input.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
int main() {
struct input_event event, event_end;
int fd = open("/dev/input/event10", O_RDWR);
if (fd < 0) {
printf("Errro open mouse:%s\n", strerror(errno));
return -1;
}
memset(&event, 0, sizeof(event));
memset(&event, 0, sizeof(event_end));
gettimeofday(&event.time, NULL);
event.type = EV_REL;
event.code = REL_X;
event.value = 100;
gettimeofday(&event_end.time, NULL);
event_end.type = EV_SYN;
event_end.code = SYN_REPORT;
event_end.value = 0;
for (int i=0; i<5; i++) {
write(fd, &event, sizeof(event));// Move the mouse
write(fd, &event_end, sizeof(event_end));// Show move
sleep(1);// wait
}
close(fd);
return 0;
}
鼠標不是環回/回聲設備。 它更像是一個終端。 您是否期望將數據寫入終端(顯示在屏幕上)以使相同的字符作為輸入返回給您? 這同樣適用於鼠標; 寫入它的唯一要點是發送改變其模式的轉義序列(例如使用的協議或分辨率)。
如果要“控制”鼠標,則必須以其他方式注入事件,或者提供fifo(命名管道)或偽tty代替/dev/psaux
以供輸入系統讀取。 然而,這可能是一種相當誤導的做事方式......
如果您解釋為什么需要控制鼠標,也許我們可以為您提供更好的替代方法來實現您的目標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.