Uinput虚拟设备被检测为android上的物理键盘

时间:2013-06-07 08:29:12

标签: android user-input linux-device-driver

我在C中创建了一个简单的本机库,允许用户使用uinput模块创建设备。代码似乎工作,但有一个问题: 我的虚拟设备被检测为物理键盘,当我需要写一些文字时,软键盘不会出现,因为android检测到连接了真正的键盘。

如何将此设备设置为虚拟?如果我没有设置keybits,它不会被检测为物理键盘,但我需要启用密钥。

#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;
}

1 个答案:

答案 0 :(得分:1)

为了解决这个问题,我制作了自定义IME: 我从SoftKeyboard包中获取了com.example.android.softkeyboard,然后我们要做的就是以这种方式覆盖onEvaluateInputViewShown方法:

@Override
public boolean onEvaluateInputViewShown() {
    return true;
}