From c45932ba7eff74cda9304629af35759d7edc499f Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 28 Aug 2017 09:54:16 -0700 Subject: [PATCH] Fixed bug 2277 - Hardware keyboard control key sequences don't get reported chw Control key sequences from hardware keyboards (wireless/USB/bluetooth) get not properly reported on Android devices. The attached patch uses the idea from http://stackoverflow.com/questions/12337117/capture-all-ctrl-under-android to make control key sequences appear as normal SDL_KEYDOWN events instead of cooked text input. --- .../src/org/libsdl/app/SDLActivity.java | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java index 7420714df..aa232dcef 100644 --- a/android-project/src/org/libsdl/app/SDLActivity.java +++ b/android-project/src/org/libsdl/app/SDLActivity.java @@ -667,6 +667,21 @@ public class SDLActivity extends Activity { return mSingleton.commandHandler.post(new ShowTextInputTask(x, y, w, h)); } + public static boolean isTextInputEvent(KeyEvent event) { + + // Key pressed with Ctrl should be sent as SDL_KEYDOWN/SDL_KEYUP and not SDL_TEXTINPUT + if (android.os.Build.VERSION.SDK_INT >= 11) { + if (event.isCtrlPressed()) { + return false; + } + } + + if (event.isPrintingKey() || event.getKeyCode() == KeyEvent.KEYCODE_SPACE) { + return true; + } + return false; + } + /** * This method is called by SDL using JNI. */ @@ -1587,23 +1602,19 @@ class DummyEdit extends View implements View.OnKeyListener { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { - - // This handles the hardware keyboard input - if (event.isPrintingKey() || keyCode == KeyEvent.KEYCODE_SPACE) { - if (event.getAction() == KeyEvent.ACTION_DOWN) { + /* + * This handles the hardware keyboard input + */ + if (event.getAction() == KeyEvent.ACTION_DOWN) { + if (SDLActivity.isTextInputEvent(event)) { ic.commitText(String.valueOf((char) event.getUnicodeChar()), 1); } - return true; - } - - if (event.getAction() == KeyEvent.ACTION_DOWN) { SDLActivity.onNativeKeyDown(keyCode); return true; } else if (event.getAction() == KeyEvent.ACTION_UP) { SDLActivity.onNativeKeyUp(keyCode); return true; } - return false; } @@ -1645,20 +1656,17 @@ class SDLInputConnection extends BaseInputConnection { @Override public boolean sendKeyEvent(KeyEvent event) { - /* - * This handles the keycodes from soft keyboard (and IME-translated - * input from hardkeyboard) + * This handles the keycodes from soft keyboard (and IME-translated input from hardkeyboard) */ int keyCode = event.getKeyCode(); if (event.getAction() == KeyEvent.ACTION_DOWN) { - if (event.isPrintingKey() || keyCode == KeyEvent.KEYCODE_SPACE) { + if (SDLActivity.isTextInputEvent(event)) { commitText(String.valueOf((char) event.getUnicodeChar()), 1); } SDLActivity.onNativeKeyDown(keyCode); return true; } else if (event.getAction() == KeyEvent.ACTION_UP) { - SDLActivity.onNativeKeyUp(keyCode); return true; }