Flutter › Flutter for Desktop (Windows, macOS, Linux)
How do you properly implement and handle keyboard shortcuts in a Flutter desktop app using the Shortcuts and Actions widgets?
IntermediateAnswer
The Shortcuts widget lets you formally define a mapping between a specific keyboard key combination, such as pressing control together with the S key, and a corresponding logical Intent object representing the actual desired action, while a separate paired Actions widget then defines exactly what genuinely happens when that specific mapped Intent is actually triggered, and this clean separation between defining a keyboard shortcut and defining the actual resulting action it performs lets you reuse and consistently trigger that exact same underlying action from multiple different sources, such as both a keyboard shortcut and a regular button click.
Shortcuts(
shortcuts: {LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyS): SaveIntent()},
child: Actions(
actions: {SaveIntent: CallbackAction(onInvoke: (_) => saveDocument())},
child: MyEditor(),
),
)Real-world example A document editing desktop app implements a control plus S keyboard shortcut to save the current document, triggering exactly the same underlying save action that a regular visible save button elsewhere in the interface also triggers.