Skip to content

Commit

Permalink
Pass text input key events to the EventResponder if they do not yield…
Browse files Browse the repository at this point in the history
… characters (flutter#20912)

If the InputConnectionAdaptor receives a key event that does not move
the caret or produce a text character (such as the back button), then
the event should be given to the EventResponder which will forward it
to the view.

Fixes flutter/flutter#64864
  • Loading branch information
jason-simmons authored Sep 1, 2020
1 parent 1bd9b8e commit d67923f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,16 @@ public boolean sendKeyEvent(KeyEvent event) {
} else {
// Enter a character.
int character = event.getUnicodeChar();
if (character != 0) {
int selStart = Math.max(0, Selection.getSelectionStart(mEditable));
int selEnd = Math.max(0, Selection.getSelectionEnd(mEditable));
int selMin = Math.min(selStart, selEnd);
int selMax = Math.max(selStart, selEnd);
if (selMin != selMax) mEditable.delete(selMin, selMax);
mEditable.insert(selMin, String.valueOf((char) character));
setSelection(selMin + 1, selMin + 1);
if (character == 0) {
return false;
}
int selStart = Math.max(0, Selection.getSelectionStart(mEditable));
int selEnd = Math.max(0, Selection.getSelectionEnd(mEditable));
int selMin = Math.min(selStart, selEnd);
int selMax = Math.max(selStart, selEnd);
if (selMin != selMax) mEditable.delete(selMin, selMax);
mEditable.insert(selMin, String.valueOf((char) character));
setSelection(selMin + 1, selMin + 1);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,17 @@ public void testSendKeyEvent_delKeyDeletesBackwardComplexEmojis() {
assertEquals(Selection.getSelectionStart(editable), 0);
}

@Test
public void testDoesNotConsumeBackButton() {
Editable editable = sampleEditable(0, 0);
InputConnectionAdaptor adaptor = sampleInputConnectionAdaptor(editable);

FakeKeyEvent keyEvent = new FakeKeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
boolean didConsume = adaptor.sendKeyEvent(keyEvent);

assertFalse(didConsume);
}

private static final String SAMPLE_TEXT =
"Lorem ipsum dolor sit amet," + "\nconsectetur adipiscing elit.";

Expand Down
3 changes: 3 additions & 0 deletions shell/platform/android/test/io/flutter/util/FakeKeyEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public FakeKeyEvent(int action, int keyCode) {
}

public final int getUnicodeChar() {
if (getKeyCode() == KeyEvent.KEYCODE_BACK) {
return 0;
}
return 1;
}
}

0 comments on commit d67923f

Please sign in to comment.