Uinput virtual device is detected as physical keyb

2019-02-20 15:00发布

问题:

I've created a simple native library in C that allows the user to create a device using uinput module. The code seems to work, but there's a problem: my virtual device is detected as physical keyboard and, when I need to write some text, the soft keyboard doesn't appear since android detects a real keyboard connected.

How to set this device virtual? If i don't set the keybits it is not detected as a physical keyboard, but I need the keys enabled.

#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:

To solve this issue I made my custom IME: I took the SoftKeyboard from com.example.android.softkeyboard package and then what there's to do is simply override the onEvaluateInputViewShown method this way:

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