问题描述
我已经创建在C语言的简单的本地库,允许用户使用uinput模块来创建一个设备。在code似乎工作,但有一个问题:我的虚拟设备被检测为物理键盘,当我需要写一些文字,软键盘由于Android没有出现检测到真正的键盘连接。
如何设置该设备的虚拟?如果我不设置它不被检测为物理键盘的keybits,但我需要启动的关键。
的#include< string.h中>
#包括< jni.h>
#包括< SYS / types.h中>
#包括< SYS / stat.h>
#包括< fcntl.h>
#包括input.h
#包括uinput.h
静态INT FD;
静态结构uinput_user_dev开发;
jint Java_com_vektor_amapper_util_InputDeviceManager_CreateVirtualDevice(
JNIEnv的* ENV,jobject THIZ,的jstring参数){
INT辅助;
FD =打开(的/ dev / uinput,O_WRONLY | O_NONBLOCK);
如果(FD℃,)
返回-1;
如果(的ioctl(FD,UI_SET_EVBIT,EV_KEY)℃的)
返回-2;
如果(的ioctl(FD,UI_SET_EVBIT,EV_REL)℃的)
返回-3;
如果(的ioctl(FD,UI_SET_EVBIT,EV_ABS)℃的)
返回-4;
如果(的ioctl(FD,UI_SET_EVBIT,EV_SYN)℃的)
返回-5;
如果(的ioctl(FD,UI_SET_EVBIT,EV_REP)℃的)
返回-6;
对(AUX = KEY_1; AUX< = KEY_F10; AUX ++){
如果(的ioctl(FD,UI_SET_KEYBIT,AUX)!= 0)返回-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的(安培;开发,0,sizeof的(发展));
为const char * cparam =(* ENV) - > GetStringUTFChars(ENV,参数,0);
的snprintf(dev.name,UINPUT_MAX_NAME_SIZE,cparam);
(* ENV) - > ReleaseStringUTFChars(ENV,参数,cparam);
dev.id.bustype = BUS_VIRTUAL;
dev.id.vendor为0x1234;
dev.id.product = 0xFEDC;
dev.id.version = 1;
如果(写(FD,与安培;开发,的sizeof(DEV))℃下)
返回-7;
如果(的ioctl(FD,UI_DEV_CREATE)℃的)
返回-8;
返回0;
}
jint Java_com_vektor_amapper_util_InputDeviceManager_DestroyVirtualDevice(
JNIEnv的* ENV,jobject THIZ){
如果(的ioctl(FD,UI_DEV_DESTROY)℃的)
返回-1;
关闭(FD);
返回0;
}
要解决这个问题,我做了我的自定义输入法:我把 SoftKeyboard
从 com.example.android.softkeyboard
包,然后什么有做的仅仅是覆盖 onEvaluateInputViewShown
的方法是这样的:
@覆盖
公共布尔onEvaluateInputViewShown(){
返回true;
}
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;
}
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;
}
这篇关于Uinput虚拟设备被检测为Android上物理键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!