Android: holding Backspace / arrow keys on the soft keyboard only fires once, because `ComponentPeerView.onKeyDown` restarts the IME on every handled key

Environment

  • JUCE 9.0.1 (tag 9.0.1), Android 16
  • Soft keyboard: Gboard
  • Component: juce::TextEditor (single line)

Summary

On Android, pressing and holding Backspace (or the Left/Right arrow keys) on the soft keyboard acts only once. The same long press works in Chrome on the same device and keyboard. Rapid repeated taps work. Only the hold, which relies on the IME’s own auto-repeat, is lost.

I traced this to ComponentPeerView.onKeyDown, which restarts the input method every time a key press is handled. I would like to request that JUCE support key hold / auto-repeat for these keys on Android.

Where the restart happens

modules/juce_gui_basics/native/java/app/com/rmsl/juce/ComponentPeerView.java:

@Override
public boolean onKeyDown (int keyCode, KeyEvent event)          // line 401
{
    ...
    // If the key press was used, we assume that the content of the focused text field has
    // changed in some way, and restart the IME to keep its context up-to-date.
    if (handleKeyDown (host, keyCode, event.getUnicodeChar(), event.getMetaState()))   // line 426
        closeInputMethodContext();                                                    // line 427
    ...
}

public void closeInputMethodContext()                           // line 354
{
    ...
    if (cachedConnection != null)
        cachedConnection.closeConnection();

    imm.restartInput (this);                                    // line 364
}

restartInput() makes the system call onCreateInputConnection() again (line 726), which creates a new Connection (line 742). So every handled Backspace or arrow key press ends the current IME session and starts a new one.

Evidence

I added a diagnostic View.OnKeyListener on the peer view. It logs each KeyEvent and the identity of ComponentPeerView.cachedConnection (read via reflection), which changes whenever the IME is restarted.

1. Normal JUCE behaviour (IME restarted on each key):

Long press, about 3 seconds, Backspace / Left / Right, in several TextEditors in my app:

  • Exactly one down/up pair arrives, then nothing, even though the key is still held.
  • connection changes between the key-down and the key-up of every pair. The restart happens right after the first key-down is handled.
  • Events from Gboard have flags=0x6 (FLAG_FROM_SYSTEM | FLAG_SOFT_KEYBOARD), repeat=0. The up event has the same eventTime as the down event.

2. Experiment: swallow the keys before JUCE sees them:

I made the listener return true for KEYCODE_DEL, KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT. onKeyDown is then never called, so the IME is never restarted. (Text is obviously not edited in this experiment. It only shows what the keyboard does.)

Key Down events in one ~3 s hold Interval connection changes
Backspace 74 to 79 about 42 ms none
Left arrow 45 to 57 about 51 ms none
Right arrow 54 to 58 about 51 ms none

With no IME restart, Gboard keeps sending key events for the whole hold. With the restart, it sends one.

For comparison, holding Backspace in Chrome (for example in the address bar) on the same device and keyboard keeps deleting characters for as long as the key is held.

Conclusion

The IME restart in onKeyDown (line 427, via closeInputMethodContext() and restartInput() at line 364) cancels the keyboard’s long-press auto-repeat. A taps-only workaround is possible on the application side, but the hold cannot be supported without a change in ComponentPeerView.

Request

Please support holding Backspace and the arrow keys on Android soft keyboards, i.e. do not tear down the IME session on every handled key press.

I have not tested the following, they are only ideas:

  • Skip closeInputMethodContext() in onKeyDown for keys that only move the caret or delete a character, and instead notify the IME of the new selection / text (for example via InputMethodManager.updateSelection()), as the existing Connection already tracks the text through its Editable.
  • Or restart the IME lazily (for example once the key is released), not on each press.
  • Or provide an opt-out, such as a flag on the peer or the TextEditor, so apps can choose.

I am happy to test any change on my devices and can share the full logs.

Related: TextEditor::mouseDown also calls peer->closeInputMethodContext() (juce_TextEditor.cpp, line 1573), but this is on a tap, so it does not affect key hold.

Thanks for reporting. This issue should be resolved on the develop branch:

Please try updating and let us know if you continue to encounter problems.