From 9022a74db3366f9795bb7e329aea11cee249c793 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 2 Oct 2023 17:10:27 +0200 Subject: [PATCH] Ported from godot: Fix Android input routing logic when using a hardware keyboard When a hardware keyboard is connected, all key events come through so we can route them directly to the engine. This is not the case for soft keyboards, for which the current logic was designed as it requires extra processing. - m4gr3d https://github.com/godotengine/godot/commit/620fdd1f078908e665a0eb2dd71306016c785abd --- .../input/PandemoniumEditText.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/platform/android/java/lib/src/org/pandemoniumengine/pandemonium/input/PandemoniumEditText.java b/platform/android/java/lib/src/org/pandemoniumengine/pandemonium/input/PandemoniumEditText.java index 157a4ddb2..3f038a900 100644 --- a/platform/android/java/lib/src/org/pandemoniumengine/pandemonium/input/PandemoniumEditText.java +++ b/platform/android/java/lib/src/org/pandemoniumengine/pandemonium/input/PandemoniumEditText.java @@ -33,6 +33,7 @@ package org.pandemoniumengine.pandemonium.input; import org.pandemoniumengine.pandemonium.*; import android.content.Context; +import android.content.res.Configuration; import android.os.Handler; import android.os.Message; import android.text.InputFilter; @@ -209,6 +210,17 @@ public class PandemoniumEditText extends EditText { @Override public boolean onKeyDown(final int keyCode, final KeyEvent keyEvent) { /* Let SurfaceView get focus if back key is input. */ + if (keyCode == KeyEvent.KEYCODE_BACK) { + mView.requestFocus(); + } + + // When a hardware keyboard is connected, all key events come through so we can route them + // directly to the engine. + // This is not the case when using a soft keyboard, requiring extra processing from this class. + if (hasHardwareKeyboard()) { + return mView.getInputHandler().onKeyDown(keyCode, keyEvent); + } + // pass event to pandemonium in special cases if (needHandlingInPandemonium(keyCode, keyEvent) && mView.getInputHandler().onKeyDown(keyCode, keyEvent)) { return true; @@ -219,6 +231,13 @@ public class PandemoniumEditText extends EditText { @Override public boolean onKeyUp(int keyCode, KeyEvent keyEvent) { + // When a hardware keyboard is connected, all key events come through so we can route them + // directly to the engine. + // This is not the case when using a soft keyboard, requiring extra processing from this class. + if (hasHardwareKeyboard()) { + return mView.getInputHandler().onKeyUp(keyCode, keyEvent); + } + if (needHandlingInPandemonium(keyCode, keyEvent) && mView.getInputHandler().onKeyUp(keyCode, keyEvent)) { return true; } else { @@ -235,10 +254,20 @@ public class PandemoniumEditText extends EditText { isModifiedKey; } + boolean hasHardwareKeyboard() { + Configuration config = getResources().getConfiguration(); + return config.keyboard != Configuration.KEYBOARD_NOKEYS && + config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO; + } + // =========================================================== // Methods // =========================================================== public void showKeyboard(String p_existing_text, VirtualKeyboardType p_type, int p_max_input_length, int p_cursor_start, int p_cursor_end) { + if (hasHardwareKeyboard()) { + return; + } + int maxInputLength = (p_max_input_length <= 0) ? Integer.MAX_VALUE : p_max_input_length; if (p_cursor_start == -1) { // cursor position not given this.mOriginText = p_existing_text; @@ -262,6 +291,10 @@ public class PandemoniumEditText extends EditText { } public void hideKeyboard() { + if (hasHardwareKeyboard()) { + return; + } + final Message msg = new Message(); msg.what = HANDLER_CLOSE_IME_KEYBOARD; msg.obj = this;