Keyboard shortcuts and focus in Flutter
flutterkeyboardshortcutsfocuscallbackshortcutsflameweb
Flutter has several ways to handle keys, and the wrong one produces handlers that silently stop firing. Researched (2025-2026 Flutter) and battle-tested in Run's level editor, skin editor, and level select screen.
Use CallbackShortcuts for app/editor shortcuts
CallbackShortcuts(
bindings: {
SingleActivator(LogicalKeyboardKey.keyZ, control: true): undo,
SingleActivator(LogicalKeyboardKey.keyZ, control: true, shift: true): redo,
SingleActivator(LogicalKeyboardKey.keyS, control: true): save,
SingleActivator(LogicalKeyboardKey.delete): deleteSelected,
},
child: Focus(autofocus: true, focusNode: _focusNode, child: Scaffold(...)),
)
- Focus aware, declarative, consumes matched events, no manual cleanup.
- It needs focus inside its subtree: keep a
Focus(autofocus: true)below it. - Focus does not come back after dialogs. After
showDialog/showModalBottomSheet,.then((_) { if (mounted) _focusNode.requestFocus(); }). - Create
FocusNodes ininitStateand dispose them; never create them inbuild(). Shortcuts+Actions+Intentsis the heavier option when actions needisEnabledor dynamic registration.
Avoid
HardwareKeyboard.instance.addHandlerfor editor shortcuts: known bugs where events stop after a TextField gains focus (Flutter #160786, 3.24.5-3.27.1), Android needing the soft keyboard opened first (#124101), macOS modifier state (#167090); and forgettingremoveHandlerstacks handlers.RawKeyboard/RawKeyEvent(deprecated) and theKeyboardListenerwidget (always returns ignored, cannot stop propagation).
Do not fire shortcuts while the user types
Bare letter shortcuts (V = select tool) will eat keystrokes in text fields. In Run the check
primaryFocus.context.widget is EditableText did not work (verified empirically). What
works: dispatch from a Focus.onKeyEvent and bail out when
primaryFocus?.context?.findAncestorWidgetOfExactType<EditableText>() != null, returning
KeyEventResult.ignored so the field still receives the key.
Global sequence detection without breaking focus (Konami code)
Wrap the screen body in an ancestor
Focus(canRequestFocus: false, skipTraversal: true, onKeyEvent: (node, e) { feed(e); return KeyEventResult.ignored; }).
Children that ignore a key let it bubble up to this node, so arrow-key navigation between
tiles keeps working while the ancestor watches for
up up down down left right left right B A. Run uses this to open a cheat-code dialog (and a
long-press on a level tile for touch devices).
Platform quirks
- Web: browsers reserve Ctrl+R/T/W and F5; focus can break with two keys pressed at once (#117841); modifier state desyncs when the tab loses focus (#115066).
- macOS:
SingleActivator(..., control: true)is Ctrl, not Cmd; usemeta:for Cmd. - Focus changes take effect a frame later;
requestFocus()is not immediate. - Physical number-row keys and numpad keys are different logical keys.
Inside a Flame game
Use HasKeyboardHandlerComponents + KeyboardHandler components for gameplay (cannot be mixed
with KeyboardEvents on the same game). GameWidget autofocuses and regains focus after
overlays close (Flame 1.4+). For rebindable controls, map logical keys to game actions in a
small service persisted to prefs (Run: 9 actions, End as a default restart key).