Pass text input key events to the EventResponder if they do not yield characters (#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 https://github.com/flutter/flutter/issues/64864
This commit is contained in:
Jason Simmons 2020-08-31 17:56:09 -07:00 committed by GitHub
parent 1bd9b8e85b
commit d67923feb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 8 deletions

View File

@ -402,15 +402,16 @@ class InputConnectionAdaptor extends BaseInputConnection {
} 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;
}
}

View File

@ -1106,6 +1106,17 @@ public class InputConnectionAdaptorTest {
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.";

View File

@ -10,6 +10,9 @@ public class FakeKeyEvent extends KeyEvent {
}
public final int getUnicodeChar() {
if (getKeyCode() == KeyEvent.KEYCODE_BACK) {
return 0;
}
return 1;
}
}