handleKeyEvent method
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 handlecontroller: The node flow controller to pass to actionscontext: 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;
}