public class AndroidKeyProcessor extends Object
KeyEventChannel
.
A class that sends Android key events to the framework, and re-dispatches those not handled by the framework.
Flutter uses asynchronous event handling to avoid blocking the UI thread, but Android requires that events are handled synchronously. So, when a key event is received by Flutter, it tells Android synchronously that the key has been handled so that it won't propagate to other components. Flutter then uses "delayed event synthesis", where it sends the event to the framework, and if the framework responds that it has not handled the event, then this class synthesizes a new event to send to Android, without handling it this time.
Constructor and Description |
---|
AndroidKeyProcessor(View view,
KeyEventChannel keyEventChannel,
TextInputPlugin textInputPlugin)
Constructor for AndroidKeyProcessor.
|
Modifier and Type | Method and Description |
---|---|
void |
destroy()
Detaches the key processor from the Flutter engine.
|
boolean |
isPendingEvent(KeyEvent event)
Returns whether or not the given event is currently being processed by this key processor.
|
boolean |
onKeyEvent(KeyEvent keyEvent)
Called when a key event is received by the
FlutterView or the InputConnectionAdaptor . |
public AndroidKeyProcessor(@NonNull View view, @NonNull KeyEventChannel keyEventChannel, @NonNull TextInputPlugin textInputPlugin)
The view is used as the destination to send the synthesized key to. This means that the the next thing in the focus chain will get the event when the framework returns false from onKeyDown/onKeyUp
It is possible that that in the middle of the async round trip, the focus chain could change, and instead of the native widget that was "next" when the event was fired getting the event, it may be the next widget when the event is synthesized that gets it. In practice, this shouldn't be a huge problem, as this is an unlikely occurrence to happen without user input, and it may actually be desired behavior, but it is possible.
view
- takes the activity to use for re-dispatching of events that were not handled by the
framework.keyEventChannel
- the event channel to listen to for new key events.textInputPlugin
- a plugin, which, if set, is given key events before the framework is,
and if it has a valid input connection and is accepting text, then it will handle the event
and the framework will not receive it.public void destroy()
The AndroidKeyProcessor instance should not be used after calling this.
public boolean onKeyEvent(@NonNull KeyEvent keyEvent)
FlutterView
or the InputConnectionAdaptor
.keyEvent
- the Android key event to respond to.public boolean isPendingEvent(@NonNull KeyEvent event)
InputConnectionAdaptor
originates
from a hardware key event, or a soft keyboard editing event.event
- the event to check for being the current event.