我在 C 中创建了一个简单的本地库,允许用户使用 uinput 模块创建设备。代码似乎可以工作,但有一个问题:我的虚拟设备被检测为物理键盘,当我需要写一些文本时,软键盘没有出现,因为 android 检测到连接了真正的键盘。
如何将此设备设置为虚拟?如果我不设置键位,它不会被检测为物理键盘,但我需要启用键。
#include <string.h>
#include <jni.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "input.h"
#include "uinput.h"
static int fd;
static struct uinput_user_dev dev;
jint Java_com_vektor_amapper_util_InputDeviceManager_CreateVirtualDevice(
JNIEnv* env, jobject thiz, jstring param) {
int aux;
fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if (fd < 0)
return -1;
if (ioctl(fd, UI_SET_EVBIT, EV_KEY)<0)
return -2;
if (ioctl(fd, UI_SET_EVBIT, EV_REL)<0)
return -3;
if (ioctl(fd, UI_SET_EVBIT, EV_ABS)<0)
return -4;
if (ioctl(fd, UI_SET_EVBIT, EV_SYN)<0)
return -5;
if (ioctl(fd, UI_SET_EVBIT, EV_REP)<0)
return -6;
for(aux = KEY_1; aux <= KEY_F10; aux++){
if(ioctl(fd,UI_SET_KEYBIT,aux)!=0) return -10;
}
ioctl(fd, UI_SET_KEYBIT, BTN_MOUSE);
ioctl(fd, UI_SET_KEYBIT, BTN_TOUCH);
ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
ioctl(fd, UI_SET_KEYBIT, BTN_MIDDLE);
ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT);
ioctl(fd, UI_SET_KEYBIT, BTN_FORWARD);
ioctl(fd, UI_SET_KEYBIT, BTN_BACK);
ioctl(fd, UI_SET_RELBIT, REL_X);
ioctl(fd, UI_SET_RELBIT, REL_Y);
ioctl (fd, UI_SET_ABSBIT, ABS_X);
ioctl (fd, UI_SET_ABSBIT, ABS_Y);
ioctl (fd, UI_SET_ABSBIT, ABS_Z);
ioctl (fd, UI_SET_ABSBIT, ABS_RX);
ioctl (fd, UI_SET_ABSBIT, ABS_RY);
ioctl (fd, UI_SET_ABSBIT, ABS_RZ);
memset(&dev, 0, sizeof(dev));
const char *cparam = (*env)->GetStringUTFChars(env, param, 0);
snprintf(dev.name, UINPUT_MAX_NAME_SIZE, cparam);
(*env)->ReleaseStringUTFChars(env, param, cparam);
dev.id.bustype = BUS_VIRTUAL;
dev.id.vendor = 0x1234;
dev.id.product = 0xFEDC;
dev.id.version = 1;
if (write(fd, &dev, sizeof(dev)) < 0)
return -7;
if (ioctl(fd, UI_DEV_CREATE) < 0)
return -8;
return 0;
}
jint Java_com_vektor_amapper_util_InputDeviceManager_DestroyVirtualDevice(
JNIEnv* env, jobject thiz) {
if (ioctl(fd, UI_DEV_DESTROY) < 0)
return -1;
close(fd);
return 0;
}