handleKeypress method

  1. @protected
KeyEventResult handleKeypress(
  1. BuildContext context,
  2. KeyEvent 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 ShortcutActivator must accept the given KeyEvent, be mapped to an Intent, the Intent must be mapped to an Action, and the Action must be enabled.

Implementation

@protected
KeyEventResult handleKeypress(BuildContext context, KeyEvent event) {
  final Intent? matchedIntent = _find(event, HardwareKeyboard.instance);
  if (matchedIntent != null) {
    final BuildContext? primaryContext = primaryFocus?.context;
    if (primaryContext != null) {
      final Action<Intent>? action = Actions.maybeFind<Intent>(
        primaryContext,
        intent: matchedIntent,
      );
      if (action != null) {
        final (bool enabled, Object? invokeResult) = Actions.of(primaryContext).invokeActionIfEnabled(
          action, matchedIntent, primaryContext,
        );
        if (enabled) {
          return action.toKeyEventResult(matchedIntent, invokeResult);
        }
      }
    }
  }
  return modal ? KeyEventResult.skipRemainingHandlers : KeyEventResult.ignored;
}