Skip to content

Commit

Permalink
Don't send fake key events while processing real ones on Android
Browse files Browse the repository at this point in the history
Fixes #11350
  • Loading branch information
slouken committed Jan 15, 2025
1 parent 1ab6163 commit e19a56f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
13 changes: 11 additions & 2 deletions android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -1507,14 +1515,15 @@ 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);
} else {
SDLInputConnection.nativeCommitText(String.valueOf((char) event.getUnicodeChar()), 1);
}
}
onNativeKeyDown(keyCode);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP) {
onNativeKeyUp(keyCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit e19a56f

Please sign in to comment.