handleKeypress method Null safety

  1. @protected
KeyEventResult handleKeypress(
  1. BuildContext context,
  2. RawKeyEvent event

Handles a key press event in the given context.

If a key mapping is found, then the associated action will be invoked using the Intent activated by the ShortcutActivator in the shortcuts map, and the currently focused widget's context (from FocusManager.primaryFocus).

Returns a KeyEventResult.handled if an action was invoked, otherwise a KeyEventResult.skipRemainingHandlers if modal is true, or if it maps to a DoNothingAction with DoNothingAction.consumesKey set to false, and in all other cases returns KeyEventResult.ignored.

In order for an action to be invoked (and KeyEventResult.handled returned), a pressed KeySet must be mapped to an Intent, the Intent must be mapped to an Action, and the Action must be enabled.


KeyEventResult handleKeypress(BuildContext context, RawKeyEvent event) {
  assert(context != null);
  final Intent? matchedIntent = _find(event, RawKeyboard.instance);
  if (matchedIntent != null) {
    final BuildContext? primaryContext = primaryFocus?.context;
    if (primaryContext != null) {
      final Action<Intent>? action = Actions.maybeFind<Intent>(
        intent: matchedIntent,
      if (action != null && action.isEnabled(matchedIntent)) {
        Actions.of(primaryContext).invokeAction(action, matchedIntent, primaryContext);
        return action.consumesKey(matchedIntent)
            ? KeyEventResult.handled
            : KeyEventResult.skipRemainingHandlers;
  return modal ? KeyEventResult.skipRemainingHandlers : KeyEventResult.ignored;