Created
September 2, 2020 12:11
-
-
Save Jonas-Sander/1d89ca856ff5bf48d987f5916b6ce99b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
// Pressing SHIFT + N (first SHIFT, then N) works on Web but not Mac | |
void main() { | |
runApp(KeyboardShortcut()); | |
} | |
class KeyboardShortcut extends StatefulWidget { | |
@override | |
_KeyboardShortcutState createState() => _KeyboardShortcutState(); | |
} | |
class _KeyboardShortcutState extends State<KeyboardShortcut> { | |
int pressed; | |
@override | |
void initState() { | |
super.initState(); | |
pressed = 0; | |
RawKeyboard.instance.addListener((event) { | |
if (event.isShiftPressed && event.isKeyPressed(LogicalKeyboardKey.keyN)) { | |
print('Pressed Shortcut'); | |
setState(() { | |
pressed += 1; | |
}); | |
} | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp(home: Center(child: Text('Pressed $pressed times.'))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment