Fix backspace not deleting original text in soft keyboard on Android

When a soft keyboard's backspace sends KEYCODE_DEL via sendKeyEvent(),
GodotEditText.onKeyDown() delegates to super.onKeyDown() which modifies
the EditText's Editable, triggering beforeTextChanged() in
GodotTextInputWrapper, which then forwards KEYCODE_DEL to the engine.

However, the EditText is only seeded with text up to the cursor position
(mOriginText) when the keyboard opens. Once the user backspaces through
all of that content, the EditText becomes empty. At that point,
super.onKeyDown(KEYCODE_DEL) on an empty EditText makes no change to the
Editable, so beforeTextChanged() never fires and the engine receives no
DEL event — even though the engine's own buffer still has text before
the cursor.

This causes a visible bug: the user can delete characters they typed in
the current session, but backspacing further into pre-existing text does
nothing. Re-tapping the field fixes it temporarily because showKeyboard()
is called again, reseeding the EditText with the current text and cursor
position.

Fixed this by forwarding KEYCODE_DEL directly to the engine when the
EditText is empty, bypassing the TextWatcher path that can't fire in
that state. This produces no double events since beforeTextChanged()
is physically unable to fire on an empty field.

Move DEL release event from onKeyDown to onKeyUp
This commit is contained in:
Voyend
2026-05-27 04:50:58 +03:00
parent c96e11965f
commit c0bb5a2d1f
@@ -239,6 +239,14 @@ public class GodotEditText extends EditText {
return mRenderView.getInputHandler().onKeyDown(keyCode, keyEvent);
}
// If EditText is empty, super.onKeyDown won't modify the Editable,
// so beforeTextChanged won't fire and Godot never receives the DEL event.
// Forward it directly in that case.
if (keyCode == KeyEvent.KEYCODE_DEL && length() == 0) {
mRenderView.getInputHandler().handleKeyEvent(KeyEvent.KEYCODE_DEL, 0, 0, true, false);
return true;
}
// pass event to godot in special cases
if (needHandlingInGodot(keyCode, keyEvent) && mRenderView.getInputHandler().onKeyDown(keyCode, keyEvent)) {
return true;
@@ -261,6 +269,12 @@ public class GodotEditText extends EditText {
return mRenderView.getInputHandler().onKeyUp(keyCode, keyEvent);
}
// Paired release event for the KEYCODE_DEL press forwarded in onKeyDown.
if (keyCode == KeyEvent.KEYCODE_DEL && length() == 0) {
mRenderView.getInputHandler().handleKeyEvent(KeyEvent.KEYCODE_DEL, 0, 0, false, false);
return true;
}
if (needHandlingInGodot(keyCode, keyEvent) && mRenderView.getInputHandler().onKeyUp(keyCode, keyEvent)) {
return true;
} else {