Don't send fake key events while processing real ones on Android

Fixes https://github.com/libsdl-org/SDL/issues/11350
This commit is contained in:
Sam Lantinga 2025-01-14 18:53:36 -08:00
parent 1ab61635a9
commit e19a56f4d5
2 changed files with 23 additions and 12 deletions

View file

@ -231,6 +231,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
protected static boolean mSDLMainFinished = false; protected static boolean mSDLMainFinished = false;
protected static boolean mActivityCreated = false; protected static boolean mActivityCreated = false;
private static SDLFileDialogState mFileDialogState = null; private static SDLFileDialogState mFileDialogState = null;
protected static boolean mDispatchingKeyEvent = false;
protected static SDLGenericMotionListener_API14 getMotionListener() { protected static SDLGenericMotionListener_API14 getMotionListener() {
if (mMotionListener == null) { if (mMotionListener == null) {
@ -807,7 +808,14 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
) { ) {
return false; 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 */ /* Transition to next state */
@ -1507,6 +1515,8 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
} }
if (event.getAction() == KeyEvent.ACTION_DOWN) { if (event.getAction() == KeyEvent.ACTION_DOWN) {
onNativeKeyDown(keyCode);
if (isTextInputEvent(event)) { if (isTextInputEvent(event)) {
if (ic != null) { if (ic != null) {
ic.commitText(String.valueOf((char) event.getUnicodeChar()), 1); ic.commitText(String.valueOf((char) event.getUnicodeChar()), 1);
@ -1514,7 +1524,6 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
SDLInputConnection.nativeCommitText(String.valueOf((char) event.getUnicodeChar()), 1); SDLInputConnection.nativeCommitText(String.valueOf((char) event.getUnicodeChar()), 1);
} }
} }
onNativeKeyDown(keyCode);
return true; return true;
} else if (event.getAction() == KeyEvent.ACTION_UP) { } else if (event.getAction() == KeyEvent.ACTION_UP) {
onNativeKeyUp(keyCode); onNativeKeyUp(keyCode);

View file

@ -111,6 +111,7 @@ public class SDLInputConnection extends BaseInputConnection
if (matchLength < text.length()) { if (matchLength < text.length()) {
String pendingText = text.subSequence(matchLength, text.length()).toString(); String pendingText = text.subSequence(matchLength, text.length()).toString();
if (!SDLActivity.dispatchingKeyEvent()) {
for (offset = 0; offset < pendingText.length(); ) { for (offset = 0; offset < pendingText.length(); ) {
int codePoint = pendingText.codePointAt(offset); int codePoint = pendingText.codePointAt(offset);
if (codePoint == '\n') { if (codePoint == '\n') {
@ -119,11 +120,12 @@ public class SDLInputConnection extends BaseInputConnection
} }
} }
/* Higher code points don't generate simulated scancodes */ /* Higher code points don't generate simulated scancodes */
if (codePoint < 128) { if (codePoint > 0 && codePoint < 128) {
nativeGenerateScancodeForUnichar((char)codePoint); nativeGenerateScancodeForUnichar((char)codePoint);
} }
offset += Character.charCount(codePoint); offset += Character.charCount(codePoint);
} }
}
SDLInputConnection.nativeCommitText(pendingText, 0); SDLInputConnection.nativeCommitText(pendingText, 0);
} }
mCommittedText = text; mCommittedText = text;