From 00e6f60cb46ebe8409594d5c5f5eeecc5109bbe8 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 14 Jan 2025 18:53:36 -0800 Subject: [PATCH] Don't send fake key events while processing real ones on Android Fixes https://github.com/libsdl-org/SDL/issues/11350 --- .../main/java/org/libsdl/app/SDLActivity.java | 13 +++++++++-- .../org/libsdl/app/SDLInputConnection.java | 22 ++++++++++--------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java index 8a1aa690b497e..55c13a1e831cd 100644 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java @@ -231,6 +231,7 @@ public enum NativeState { protected static boolean mSDLMainFinished = false; protected static boolean mActivityCreated = false; private static SDLFileDialogState mFileDialogState = null; + protected static boolean mDispatchingKeyEvent = false; protected static SDLGenericMotionListener_API14 getMotionListener() { if (mMotionListener == null) { @@ -807,7 +808,14 @@ public boolean dispatchKeyEvent(KeyEvent event) { ) { return false; } - return super.dispatchKeyEvent(event); + mDispatchingKeyEvent = true; + boolean result = super.dispatchKeyEvent(event); + mDispatchingKeyEvent = false; + return result; + } + + public static boolean dispatchingKeyEvent() { + return mDispatchingKeyEvent; } /* Transition to next state */ @@ -1508,6 +1516,8 @@ public static boolean handleKeyEvent(View v, int keyCode, KeyEvent event, InputC } if (event.getAction() == KeyEvent.ACTION_DOWN) { + onNativeKeyDown(keyCode); + if (isTextInputEvent(event)) { if (ic != null) { ic.commitText(String.valueOf((char) event.getUnicodeChar()), 1); @@ -1515,7 +1525,6 @@ public static boolean handleKeyEvent(View v, int keyCode, KeyEvent event, InputC SDLInputConnection.nativeCommitText(String.valueOf((char) event.getUnicodeChar()), 1); } } - onNativeKeyDown(keyCode); return true; } else if (event.getAction() == KeyEvent.ACTION_UP) { onNativeKeyUp(keyCode); diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLInputConnection.java b/android-project/app/src/main/java/org/libsdl/app/SDLInputConnection.java index e1d29a8890e2d..accce4ba3f657 100644 --- a/android-project/app/src/main/java/org/libsdl/app/SDLInputConnection.java +++ b/android-project/app/src/main/java/org/libsdl/app/SDLInputConnection.java @@ -111,18 +111,20 @@ protected void updateText() { if (matchLength < text.length()) { String pendingText = text.subSequence(matchLength, text.length()).toString(); - for (offset = 0; offset < pendingText.length(); ) { - int codePoint = pendingText.codePointAt(offset); - if (codePoint == '\n') { - if (SDLActivity.onNativeSoftReturnKey()) { - return; + if (!SDLActivity.dispatchingKeyEvent()) { + for (offset = 0; offset < pendingText.length(); ) { + int codePoint = pendingText.codePointAt(offset); + if (codePoint == '\n') { + if (SDLActivity.onNativeSoftReturnKey()) { + return; + } } + /* Higher code points don't generate simulated scancodes */ + if (codePoint > 0 && codePoint < 128) { + nativeGenerateScancodeForUnichar((char)codePoint); + } + offset += Character.charCount(codePoint); } - /* Higher code points don't generate simulated scancodes */ - if (codePoint < 128) { - nativeGenerateScancodeForUnichar((char)codePoint); - } - offset += Character.charCount(codePoint); } SDLInputConnection.nativeCommitText(pendingText, 0); }