handleKeyEvent method

bool handleKeyEvent(
  1. KeyEvent event,
  2. NodeFlowController<T> controller,
  3. BuildContext? context
)

Handles keyboard events and executes matching actions.

Call this from your keyboard event handler to process shortcuts. Only responds to KeyDownEvent to prevent duplicate executions.

Parameters:

  • event: The keyboard event to handle
  • controller: The node flow controller to pass to actions
  • context: Optional build context for actions that need it

Returns: true if an action was found and executed, false otherwise

Example:

Focus(
  onKeyEvent: (node, event) {
    if (manager.handleKeyEvent(event, controller, context)) {
      return KeyEventResult.handled;
    }
    return KeyEventResult.ignored;
  },
  child: ...,
);

Implementation

bool handleKeyEvent(
  KeyEvent event,
  NodeFlowController<T> controller,
  BuildContext? context,
) {
  if (event is! KeyDownEvent) return false;

  // Normalize pressed keys to handle left/right variants of modifier keys
  final allPressedKeys = {
    event.logicalKey,
    ...HardwareKeyboard.instance.logicalKeysPressed,
  };

  final normalizedPressedKeys = _normalizeKeySet(allPressedKeys);
  final pressedKeys = LogicalKeySet.fromSet(normalizedPressedKeys);

  // Direct lookup - O(1) instead of O(n)
  final actionId = _shortcuts[pressedKeys];
  if (actionId != null) {
    final action = _actions[actionId];
    if (action != null && action.canExecute(controller)) {
      final result = action.execute(controller, context);
      return result;
    }
  }

  return false;
}