keyMessageHandler property Null safety

KeyMessageHandler? keyMessageHandler
read / write

The global handler for all hardware key messages of Flutter.

Key messages received from the platform are first sent to RawKeyboard's listeners and HardwareKeyboard's handlers, then sent to keyMessageHandler, regardless of the results of HardwareKeyboard's handlers. The event results from the handlers and keyMessageHandler are combined and returned to the platform. The event result is explained below.

For most common applications, which use WidgetsBinding, this field is set by the focus system (see FocusManger) on startup and should not be change explicitly.

If you are not using the focus system to manage focus, set this attribute to a KeyMessageHandler that returns true if the propagation on the platform should not be continued. If this field is null, key events will be assumed to not have been handled by Flutter.

Event result

Key messages on the platform are given to Flutter to be handled by the engine. If they are not handled, then the platform will continue to distribute the keys (i.e. propagate them) to other (possibly non-Flutter) components in the application. The return value from this handler tells the platform to either stop propagation (by returning true: "event handled"), or pass the event on to other controls (false: "event not handled"). Some platforms might trigger special alerts if the event is not handled by other controls either (such as the "bonk" noise on macOS).

The result from keyMessageHandler and HardwareKeyboard's handlers are combined. If any of the handlers claim to handle the event, the overall result will be "event handled".

See also:

Implementation

KeyMessageHandler? keyMessageHandler;