Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 64cac5c

Browse files
committed
Switch to dispatchKeyEvent instead of dispatchKeyEventPreIme
1 parent 0043368 commit 64cac5c

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ public void destroy() {
9191
public boolean onKeyEvent(@NonNull KeyEvent keyEvent) {
9292
int action = keyEvent.getAction();
9393
if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_UP) {
94-
// There is theoretically a KeyEvent.ACTION_MULTIPLE, but that shouldn't
95-
// be sent anymore anyhow.
94+
// There is theoretically a KeyEvent.ACTION_MULTIPLE, but theoretically
95+
// that isn't sent by Android anymore, so this is just for protection in
96+
// case the theory is wrong.
9697
return false;
9798
}
9899
if (eventResponder.dispatchingKeyEvent) {
@@ -109,14 +110,7 @@ public boolean onKeyEvent(@NonNull KeyEvent keyEvent) {
109110
keyEventChannel.keyUp(flutterEvent);
110111
}
111112
eventResponder.addEvent(flutterEvent.eventId, keyEvent);
112-
113-
// We can't return "true" for system keys, since Android won't re-dispatch
114-
// it later to the IME when we call dispatchKeyEventPreIme with it, even if
115-
// we send it to our root view. Sadly, this means that even if you handle a
116-
// system key (like the back button) in Flutter, the button press still gets
117-
// propagated to the IME, etc., and you can't block the soft keyboard from
118-
// being dismissed.
119-
return !keyEvent.isSystem();
113+
return true;
120114
}
121115

122116
/**
@@ -270,10 +264,8 @@ public void dispatchKeyEvent(KeyEvent event) {
270264
}
271265
}
272266

273-
// Since the framework didn't handle it, dispatch the event again, unless
274-
// it's a system key, which has already been returned to the system as not
275-
// handled in onKeyEvent, above.
276-
if (view != null && !event.isSystem()) {
267+
// Since the framework didn't handle it, dispatch the event again.
268+
if (view != null) {
277269
// Turn on dispatchingKeyEvent so that we don't dispatch to ourselves and
278270
// send it to the framework again.
279271
dispatchingKeyEvent = true;

shell/platform/android/io/flutter/embedding/android/FlutterView.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -721,36 +721,36 @@ public boolean checkInputConnectionProxy(View view) {
721721
}
722722

723723
/**
724-
* Invoked when a hardware key is pressed or released, before the IME receives the key.
724+
* Invoked when a hardware key is pressed or released.
725725
*
726726
* <p>This method is typically invoked in response to the press of a physical keyboard key or a
727727
* D-pad button. It is generally not invoked when a virtual software keyboard is used, though a
728-
* software keyboard may choose to invoke this method in some situations,
729-
* notably the back button often causes this function to be invoked.
728+
* software keyboard may choose to invoke this method in some situations.
730729
*
731730
* <p>{@link KeyEvent}s are sent from Android to Flutter. {@link AndroidKeyProcessor} may do some
732731
* additional work with the given {@link KeyEvent}, e.g., combine this {@code keyCode} with the
733732
* previous {@code keyCode} to generate a unicode combined character.
734733
*/
735734
@Override
736-
public boolean dispatchKeyEventPreIme(KeyEvent event) {
735+
public boolean dispatchKeyEvent(KeyEvent event) {
737736
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
738737
// Tell Android to start tracking this event.
739738
getKeyDispatcherState().startTracking(event, this);
740739
} else if (event.getAction() == KeyEvent.ACTION_UP) {
740+
// Stop tracking the event.
741741
getKeyDispatcherState().handleUpEvent(event);
742742
if (!event.isTracking() || event.isCanceled()) {
743743
// Don't send the event to the key processor if it was canceled, or no
744744
// longer being tracked.
745-
return super.dispatchKeyEventPreIme(event);
745+
return super.dispatchKeyEvent(event);
746746
}
747747
}
748748
// If the key processor doesn't handle it, then send it on to the
749749
// superclass. The key processor will typically handle all events except
750750
// those where it has re-dispatched the event after receiving a reply from
751751
// the framework that the framework did not handle it.
752752
return (isAttachedToFlutterEngine() && androidKeyProcessor.onKeyEvent(event))
753-
|| super.dispatchKeyEventPreIme(event);
753+
|| super.dispatchKeyEvent(event);
754754
}
755755

756756
/**

0 commit comments

Comments
 (0)