20090826 Android上使用觸控面板(TouchScreen)模擬KeyEvent (三)
最後就是模擬Key的部份了,其實比讀取還簡單,這都得歸功於Input Subsystem的設計,Service只要呼叫EventReader.SimulateKey(KeyboardFD, code); 這裡的KeyboardFD 就是/dev/input/eventX,而Code則是可以去看 KernelSource/include/linux/input.h 裡面有提到 Backspace = 14, 下面是c的code 1: JNIEXPORT jint JNICALL Java_ITRI_MTube_EventReader_SimulateKey(JNIEnv *env, jclass c, jstring KbdFD, jshort code) 2: { 3: struct input_event event; 4: int fd; 5: const jbyte *FD; 6: FD = (*env)->GetStringUTFChars(env, KbdFD, NULL); 7: if (FD == NULL) { 8: return NULL; /* OutOfMemoryError already thrown */ 9: } 10: 11: if ((fd = open(FD, O_RDWR)) < 0) { 12: (*env)->ReleaseStringUTFChars(env, KbdFD, FD); 13: return -1; 14: } 15: 16: (*env)->ReleaseStringUTFChars(env, KbdFD, FD); 17: event.type = EV_KEY; 18: event.code = code; 19: //keydown 20: event.value = 1; 21: if (write(fd, &event, sizeof(event)) < 0){ 22: ...