我試圖在Android上使用/ dev/uinput和ioctl移動光標鼠標。Android鼠標光標事件/ dev/uinput不能在Y軸上移動
這裏是我的代碼:
int fd = -1;
struct input_event ev;
int uinput_open_device() {
fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if (fd < 0) {
__android_log_print(ANDROID_LOG_DEBUG, "Code C", "Failed to Open Event");
return -1;
}
return fd;
}
int dev_uinput_init_mouse(char *name) {
struct uinput_user_dev dev;
fd = uinput_open_device();
if (fd > 0) {
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
ioctl(fd, UI_SET_EVBIT, EV_REL);
ioctl(fd, UI_SET_RELBIT, REL_X);
ioctl(fd, UI_SET_RELBIT, REL_Y);
memset(&dev, 0, sizeof(dev));
strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE);
dev.id.bustype = BUS_USB;
dev.id.vendor = 0x1;
dev.id.product = 0x1;
dev.id.version = 1;
if (write(fd, &dev, sizeof(dev)) < 0) {
__android_log_print(ANDROID_LOG_DEBUG, "Code C", "Failed to write");
close(fd);
}
if (ioctl(fd, UI_DEV_CREATE) < 0) {
__android_log_print(ANDROID_LOG_DEBUG, "IOCTL", "Create Failed...");
close(fd);
return -1;
}
}
return fd;
void dev_uinput_sync(int fd) {
memset(&ev, 0, sizeof(struct input_event));
ev.type = EV_SYN;
ev.code = SYN_REPORT;
ev.value = 0;
if (write(fd, &ev, sizeof(struct input_event)) < 0) {
__android_log_print(ANDROID_LOG_DEBUG, "Code C", "Failed to write");
} else {
__android_log_print(ANDROID_LOG_DEBUG, "Code C", "Sync OK !");
}
}
void ptr_abs(int fd, int x, int y) {
memset(&ev, 0, sizeof(struct input_event));
ev.type = EV_REL;
ev.code = REL_X;
ev.value = x;
write(fd, &ev, sizeof(struct input_event))
memset(&ev, 0, sizeof(struct input_event));
ev.type = EV_REL;
ev.code = REL_Y;
ev.value = y;
write(fd, &ev, sizeof(struct input_event);
dev_uinput_sync(fd);
}
void dev_uinput_close(int fd) {
ioctl(fd, UI_DEV_DESTROY);
close(fd);
}
JNIEXPORT void JNICALL
Java_com_example_hellojni_HelloJni_mouseSimulation(JNIEnv *env, jobject instance, jint absX, jint absY) {
ptr_abs(fd, absX, absY);
}
JNIEXPORT void JNICALL
Java_com_example_hellojni_HelloJni_openFD(JNIEnv *env, jobject instance) {
dev_uinput_init_mouse("uinput-sample-test");
}
JNIEXPORT void JNICALL
Java_com_example_hellojni_HelloJni_closeFD(JNIEnv *env, jobject instance) {
dev_uinput_close(fd);
}
所有這些功能都是通過在我的Android應用程序的服務調用。 函數Java_com_example_hellojni_HelloJni_mouseSimulation(int Xaxis, int Yaxis)
用Xaxis和Yaxis的值調用,但在屏幕上,我們可以看到鼠標僅在X軸上移動,而不在Y軸上移動。
例如,如果我撥打Java_com_example_hellojni_HelloJni_mouseSimulation(200, 100)
,光標將在X軸上移動200個像素,在Y軸上移動任何內容。
正如你所看到的運動是由函數void ptr_abs(int fd, int x, int y)
在功能上並沒有失敗的write
執行。我在REL_Y
函數上使用ioctl
啓用了Y軸上的運動。
我正在編譯Android API 19上的此代碼。
謝謝你的幫忙!
爲什麼要用'C'標籤? – VatsalSura
因爲這是一個C代碼? – DevAndroid
您的代碼是否正在編譯?它看起來像你在這裏錯過了一個括號:write(fd,&ev,sizeof(struct input_event);(它正在寫REL_Y事件) – samgak