int WindowImplAndroid::getUnicode(AInputEvent* event) { // Retrieve activity states ActivityStates* states = getActivity(NULL); Lock lock(states->mutex); // Initializes JNI jint lResult; jint lFlags = 0; JavaVM* lJavaVM = states->activity->vm; JNIEnv* lJNIEnv = states->activity->env; JavaVMAttachArgs lJavaVMAttachArgs; lJavaVMAttachArgs.version = JNI_VERSION_1_6; lJavaVMAttachArgs.name = "NativeThread"; lJavaVMAttachArgs.group = NULL; lResult=lJavaVM->AttachCurrentThread(&lJNIEnv, &lJavaVMAttachArgs); if (lResult == JNI_ERR) err() << "Failed to initialize JNI, couldn't get the Unicode value" << std::endl; // Retrieve key data from the input event jlong downTime = AKeyEvent_getDownTime(event); jlong eventTime = AKeyEvent_getEventTime(event); jint action = AKeyEvent_getAction(event); jint code = AKeyEvent_getKeyCode(event); jint repeat = AKeyEvent_getRepeatCount(event); // not sure! jint metaState = AKeyEvent_getMetaState(event); jint deviceId = AInputEvent_getDeviceId(event); jint scancode = AKeyEvent_getScanCode(event); jint flags = AKeyEvent_getFlags(event); jint source = AInputEvent_getSource(event); // Construct a KeyEvent object from the event data jclass ClassKeyEvent = lJNIEnv->FindClass("android/view/KeyEvent"); jmethodID KeyEventConstructor = lJNIEnv->GetMethodID(ClassKeyEvent, "<init>", "(JJIIIIIIII)V"); jobject ObjectKeyEvent = lJNIEnv->NewObject(ClassKeyEvent, KeyEventConstructor, downTime, eventTime, action, code, repeat, metaState, deviceId, scancode, flags, source); // Call its getUnicodeChar() method to get the Unicode value jmethodID MethodGetUnicode = lJNIEnv->GetMethodID(ClassKeyEvent, "getUnicodeChar", "(I)I"); int unicode = lJNIEnv->CallIntMethod(ObjectKeyEvent, MethodGetUnicode, metaState); lJNIEnv->DeleteLocalRef(ClassKeyEvent); lJNIEnv->DeleteLocalRef(ObjectKeyEvent); // Detach this thread from the JVM lJavaVM->DetachCurrentThread(); return unicode; }
bool InputService::onKeyboardEvent(AInputEvent* pEvent) { #ifdef INPUTSERVICE_LOG_EVENTS packt_Log_debug("AKeyEvent_getAction=%d", AKeyEvent_getAction(pEvent)); packt_Log_debug("AKeyEvent_getFlags=%d", AKeyEvent_getFlags(pEvent)); packt_Log_debug("AKeyEvent_getKeyCode=%d", AKeyEvent_getKeyCode(pEvent)); packt_Log_debug("AKeyEvent_getScanCode=%d", AKeyEvent_getScanCode(pEvent)); packt_Log_debug("AKeyEvent_getMetaState=%d", AKeyEvent_getMetaState(pEvent)); packt_Log_debug("AKeyEvent_getRepeatCount=%d", AKeyEvent_getRepeatCount(pEvent)); packt_Log_debug("AKeyEvent_getDownTime=%lld", AKeyEvent_getDownTime(pEvent)); packt_Log_debug("AKeyEvent_getEventTime=%lld", AKeyEvent_getEventTime(pEvent)); #endif const float ORTHOGONAL_MOVE = 1.0f; if (AKeyEvent_getAction(pEvent) == AKEY_EVENT_ACTION_DOWN) { switch (AKeyEvent_getKeyCode(pEvent)) { case AKEYCODE_DPAD_LEFT: mHorizontal = -ORTHOGONAL_MOVE; break; case AKEYCODE_DPAD_RIGHT: mHorizontal = ORTHOGONAL_MOVE; break; case AKEYCODE_DPAD_DOWN: mVertical = -ORTHOGONAL_MOVE; break; case AKEYCODE_DPAD_UP: mVertical = ORTHOGONAL_MOVE; break; case AKEYCODE_BACK: return false; } } else { switch (AKeyEvent_getKeyCode(pEvent)) { case AKEYCODE_DPAD_LEFT: case AKEYCODE_DPAD_RIGHT: mHorizontal = 0.0f; break; case AKEYCODE_DPAD_DOWN: case AKEYCODE_DPAD_UP: mVertical = 0.0f; break; case AKEYCODE_MENU: mMenuKey = true; break; case AKEYCODE_BACK: return false; } } return true; }
extern "C" int AndroidGetKeyText( AInputEvent *event ) { int result = 0; // Attaches the current thread to the JVM. jint lResult; jint lFlags = 0; JavaVM* lJavaVM = engine.app->activity->vm; JNIEnv* lJNIEnv = engine.app->activity->env; JavaVMAttachArgs lJavaVMAttachArgs; lJavaVMAttachArgs.version = JNI_VERSION_1_6; lJavaVMAttachArgs.name = "NativeThread"; lJavaVMAttachArgs.group = NULL; lResult=lJavaVM->AttachCurrentThread(&lJNIEnv, &lJavaVMAttachArgs); if (lResult == JNI_ERR) { return 0; } // Retrieves NativeActivity. jobject lNativeActivity = engine.app->activity->clazz; jclass ClassNativeActivity = lJNIEnv->GetObjectClass(lNativeActivity); jmethodID MethodSetFlags = lJNIEnv->GetMethodID( ClassNativeActivity, "getKeyText", "(JJIIIIIII)I"); lprintf( "..." ); if( MethodSetFlags ) result = lJNIEnv->CallIntMethod( lNativeActivity, MethodSetFlags , AKeyEvent_getDownTime(event) , AKeyEvent_getEventTime(event) , AKeyEvent_getAction(event) , AKeyEvent_getKeyCode(event) , AKeyEvent_getRepeatCount(event) , AKeyEvent_getMetaState(event) , AInputEvent_getDeviceId(event) , AKeyEvent_getScanCode(event) , AKeyEvent_getFlags(event) ); else { lprintf( "Failed to get method." ); result = 0; } // Finished with the JVM. lJavaVM->DetachCurrentThread(); return result; }
static int32_t handle_key_event(struct android_app* app, AInputEvent* event) { struct engine* engine = (struct engine*)app->userData; int32_t action = AKeyEvent_getAction(event); int32_t keyCode = AKeyEvent_getKeyCode(event); int32_t scanCode = AKeyEvent_getScanCode(event); int32_t flags = AKeyEvent_getFlags(event); if (action == AKEY_EVENT_ACTION_DOWN) { return onWindowKeyDown(keyCode, scanCode) ? 0 : 1; } else if (action == AKEY_EVENT_ACTION_UP) { if ((flags & AKEY_EVENT_FLAG_CANCELED) == 0) { return onWindowKeyUp(keyCode, scanCode) ? 0 : 1; } } else if (action == AKEY_EVENT_ACTION_MULTIPLE) { LOGI("AKEY_EVENT_ACTION_MULTIPLE received"); } return 0; }
bool AndroidGameApplication::onKeyEvent(AInputEvent* event) { int32 action = AKeyEvent_getAction(event); int32 flags = AKeyEvent_getFlags(event); int32 keyCode = AKeyEvent_getKeyCode(event); int32 scanCode = AKeyEvent_getScanCode(event); int32 state = AKeyEvent_getMetaState(event); int32 repeatCount = AKeyEvent_getRepeatCount(event); LOGI("AndroidGameApplication::onKeyEvent"); switch(action) { case AKEY_EVENT_ACTION_DOWN: { LOGI("AKEY_EVENT_ACTION_DOWN [keyCode = %d, scanCode = %d, repeatCount = %d, flags = 0x%X, state = 0x%X]", keyCode, scanCode, repeatCount, flags, state); for(std::list<IKeyboardController*>::iterator it = m_keyboardInputHandlers.begin(); it != m_keyboardInputHandlers.end(); ++it) { (*it)->onKeyDown(keyCode, flags); } } break; case AKEY_EVENT_ACTION_UP: { LOGI("AKEY_EVENT_ACTION_DOWN [keyCode = %d, scanCode = %d, repeatCount = %d, flags = 0x%X, state = 0x%X]", keyCode, scanCode, repeatCount, flags, state); for(std::list<IKeyboardController*>::iterator it = m_keyboardInputHandlers.begin(); it != m_keyboardInputHandlers.end(); ++it) { (*it)->onKeyUp(keyCode, flags); } } break; } return true; }
bool OuyaInputView::dispatchKeyEvent(AInputEvent* keyEvent) { int64_t downTime = AKeyEvent_getDownTime(keyEvent); int64_t eventTime = AKeyEvent_getEventTime(keyEvent); int32_t action = AKeyEvent_getAction(keyEvent); int32_t code = AKeyEvent_getKeyCode(keyEvent); int32_t repeat = AKeyEvent_getRepeatCount(keyEvent); int32_t metaState = AKeyEvent_getMetaState(keyEvent); int32_t deviceId = AInputEvent_getDeviceId(keyEvent); int32_t scancode = AKeyEvent_getScanCode(keyEvent); int32_t flags = AKeyEvent_getFlags(keyEvent); int32_t source = AInputEvent_getSource(keyEvent); #if ENABLE_VERBOSE_LOGGING __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "downTime=%lld eventTime=%lld action=%d code=%d repeat=%d metaState=%d deviceId=%d scancode=%d flags=%d source=%d", downTime, eventTime, action, code, repeat, metaState, deviceId, scancode, flags, source); #endif return javaDispatchKeyEvent(downTime, eventTime, action, code, repeat, metaState, deviceId, scancode, flags, source); }
/** * Process the next input event. */ static int32_t window_handle_input(struct android_app* app, AInputEvent* event) { la_window_t* window = (la_window_t*)app->userData; int32_t input_type = AInputEvent_getType(event); if (input_type == AINPUT_EVENT_TYPE_MOTION) { return 1; }else if(input_type == AINPUT_EVENT_TYPE_KEY) { la_print("KEY EVENT"); int32_t key_code = AKeyEvent_getKeyCode(event); switch(key_code) { case AKEYCODE_BACK: break; case AKEYCODE_CAMERA: la_print("Camera key pressed"); break; case AKEYCODE_MENU: la_print("Menu key pressed"); break; case AKEYCODE_DEL: la_print("Backspace key pressed"); break; case AKEYCODE_ENTER: la_print("Enter key pressed"); break; default: if(key_code >= AKEYCODE_A && key_code <= AKEYCODE_PERIOD) { char key = AKeyEvent_getScanCode(event); la_print("Key Insert %s", &key); } break; } } return 1; }
static int32_t _onInputEvent(struct android_app* app, AInputEvent* event) { if (!event || !::g_defaultWindow.get()) { return 1; } int32_t source = AInputEvent_getSource(event); if (source == AINPUT_SOURCE_MOUSE) { int32_t x = (int32_t)AMotionEvent_getX(event, 0); int32_t y = (int32_t)AMotionEvent_getY(event, 0); g_defaultWindow->getMouseInput().setLocation(glm::ivec2(x, y)); int32_t buttonState = AMotionEvent_getButtonState(event); g_defaultWindow->getMouseInput().setButton(VKTS_MOUSE_BUTTON_LEFT, buttonState & AMOTION_EVENT_BUTTON_PRIMARY); g_defaultWindow->getMouseInput().setButton(VKTS_MOUSE_BUTTON_RIGHT, buttonState & AMOTION_EVENT_BUTTON_SECONDARY); g_defaultWindow->getMouseInput().setButton(VKTS_MOUSE_BUTTON_MIDDLE, buttonState & AMOTION_EVENT_BUTTON_TERTIARY); int32_t vscroll = (int32_t)AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_VSCROLL, 0); g_defaultWindow->getMouseInput().setMouseWheel(vscroll); return 1; } else if (source == AINPUT_SOURCE_KEYBOARD) { VkBool32 pressed = (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_DOWN); int32_t scanCode = AKeyEvent_getScanCode(event); int32_t key = vkts::_visualTranslateKey(scanCode); if (pressed && key == VKTS_KEY_ESCAPE) { g_app->destroyRequested = 1; } else if (key != VKTS_KEY_UNKNOWN) { g_defaultWindow->getKeyInput().setKey(key, pressed); } return 1; } else if (source == AINPUT_SOURCE_JOYSTICK) { int32_t pointerIndex = (AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; if (pointerIndex >= 0 && pointerIndex < VKTS_MAX_GAMEPADS) { for (int32_t axis = AMOTION_EVENT_AXIS_X; axis <= AMOTION_EVENT_AXIS_RTRIGGER; axis++) { int32_t axisIndex = -1; float axisValue = AMotionEvent_getAxisValue(event, axis, pointerIndex); switch (axis) { case AMOTION_EVENT_AXIS_X: axisIndex = VKTS_GAMEPAD_LEFT_STICK_X; break; case AMOTION_EVENT_AXIS_Y: axisIndex = VKTS_GAMEPAD_LEFT_STICK_Y; axisValue = -axisValue; break; case AMOTION_EVENT_AXIS_Z: axisIndex = VKTS_GAMEPAD_RIGHT_STICK_X; break; case AMOTION_EVENT_AXIS_RZ: axisIndex = VKTS_GAMEPAD_RIGHT_STICK_Y; axisValue = -axisValue; break; case AMOTION_EVENT_AXIS_LTRIGGER: axisIndex = VKTS_GAMEPAD_LEFT_TRIGGER; break; case AMOTION_EVENT_AXIS_RTRIGGER: axisIndex = VKTS_GAMEPAD_RIGHT_TRIGGER; break; case AMOTION_EVENT_AXIS_HAT_X: { VkBool32 buttonPressed = axisValue < 0.0f ? VK_TRUE : VK_FALSE; g_defaultWindow->getGamepadInput(pointerIndex).setButton(VKTS_GAMEPAD_DPAD_LEFT, buttonPressed); buttonPressed = axisValue > 0.0f ? VK_TRUE : VK_FALSE; g_defaultWindow->getGamepadInput(pointerIndex).setButton(VKTS_GAMEPAD_DPAD_RIGHT, buttonPressed); } break; case AMOTION_EVENT_AXIS_HAT_Y: { VkBool32 buttonPressed = axisValue > 0.0f ? VK_TRUE : VK_FALSE; g_defaultWindow->getGamepadInput(pointerIndex).setButton(VKTS_GAMEPAD_DPAD_DOWN, buttonPressed); buttonPressed = axisValue < 0.0f ? VK_TRUE : VK_FALSE; g_defaultWindow->getGamepadInput(pointerIndex).setButton(VKTS_GAMEPAD_DPAD_UP, buttonPressed); } break; } if (axisIndex == -1) { continue; } g_defaultWindow->getGamepadInput(pointerIndex).setAxis(axisIndex, axisValue); } } return 1; }
int32_t onKey(const int32_t& action, void* data) { auto event = static_cast<AInputEvent*>(data); const int32_t metakey = AKeyEvent_getMetaState(event); const int32_t key = AKeyEvent_getKeyCode(event); const int32_t scanCode = AKeyEvent_getScanCode(event); int jopKey = Input::getJopKey(key); const int mod = ((metakey & AMETA_SHIFT_ON) != 0) * Keyboard::Modifier::Shift | ((metakey & AMETA_ALT_ON) != 0) * Keyboard::Modifier::Alt | ((metakey & AMETA_CTRL_ON) != 0) * Keyboard::Modifier::Control; int32_t device = AInputEvent_getSource(event); auto& windowRef = Engine::getMainWindow(); switch (action) { case AKEY_EVENT_ACTION_DOWN: { auto state = ActivityState::get(); if (jopKey != Keyboard::Unknown) { state->activeKey = jopKey; windowRef.getEventHandler()->keyPressed(jopKey, scanCode, mod); } if (Controller::controllersPresent()) { jopKey = Input::getJopControllerButton(key); if (jopKey != Controller::XBox::Unknown) { state->activeControllerButtons[jopKey] = true; windowRef.getEventHandler()->controllerButtonPressed(0, jopKey); } } return 1; } case AKEY_EVENT_ACTION_UP: { auto state = ActivityState::get(); if (jopKey != Keyboard::Unknown) { state->activeKey = -1; windowRef.getEventHandler()->keyReleased(jopKey, scanCode, mod); } if (Controller::controllersPresent()) { jopKey = Input::getJopControllerButton(key); if (jopKey != Controller::XBox::Unknown) { state->activeControllerButtons[jopKey] = false; windowRef.getEventHandler()->controllerButtonReleased(0, jopKey); } } return 1; } case AKEY_EVENT_ACTION_MULTIPLE: { if (jopKey != 0) { windowRef.getEventHandler()->keyPressed(jopKey, scanCode, mod); windowRef.getEventHandler()->keyReleased(jopKey, scanCode, mod); } if (Controller::controllersPresent()) { jopKey = Input::getJopControllerButton(key); if (jopKey != Controller::XBox::Unknown) { windowRef.getEventHandler()->controllerButtonPressed(0, jopKey); windowRef.getEventHandler()->controllerButtonReleased(0, jopKey); } } return 1; } } return 0; }