Keyboard shortcuts on Flutter
Most people operate their smartphones with touch, but some people use an external keyboard. Learning and using keyboard shortcuts can save you a lot of time. Shortcuts should always use a modifier key, such as CTRL
or CMD
to avoid clashing with system shortcuts.
With Flutter, you can use the RawKeyboard
listener to implement shortcuts in your app. The RawKeyboard
listener yields a RawKeyUpEvent
of a RawKeyDownEvent
. The data
attribute has a isModifierPressed()
method that can be used to determine whether a modifier key has been pressed.
RawKeyboard.instance.addListener((keyEvent) {
if (keyEvent is RawKeyUpEvent) {
if (keyEvent.logicalKey == LogicalKeyboardKey.keyF &&
keyEvent.data.isModifierPressed(ModifierKey.controlModifier)) {
find();
}
}
});
void find() {
// Logic
}