c-Android鼠标光标事件/dev/uinput未在Y轴上移动



我正在尝试使用/dev/uinput和ioctl在Android上移动光标鼠标。

这是我的代码:

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轴上的移动。

我正在安卓API 19上编译这段代码。

谢谢你的帮助!

问题解决了。。。我删除了每个写函数中的每个__android_log_print和条件,它起作用了。。。

奇怪。。。

最新更新