-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
hotkey_manager.dart
139 lines (124 loc) · 4.16 KB
/
hotkey_manager.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import 'dart:async';
import 'package:collection/collection.dart';
import 'package:flutter/services.dart';
import 'package:hotkey_manager_platform_interface/hotkey_manager_platform_interface.dart';
class HotKeyManager {
HotKeyManager._() {
_platform.onKeyEventReceiver.listen(_handleSystemHotKeyEvent);
HardwareKeyboard.instance.addHandler(_handleInAppHotKeyEvent);
}
/// The shared instance of [HotKeyManager].
static final HotKeyManager instance = HotKeyManager._();
HotKeyManagerPlatform get _platform => HotKeyManagerPlatform.instance;
final List<HotKey> _hotKeyList = [];
final Map<String, HotKeyHandler> _keyDownHandlerMap = {};
final Map<String, HotKeyHandler> _keyUpHandlerMap = {};
HotKey? _lastPressedHotKey;
/// Handle system hot key event.
void _handleSystemHotKeyEvent(event) {
String type = event['type'] as String;
Map<Object?, Object?> data = event['data'] as Map;
String identifier = data['identifier'] as String;
HotKey? hotKey = _hotKeyList.firstWhereOrNull(
(e) => e.identifier == identifier,
);
if (hotKey != null) {
switch (type) {
case 'onKeyDown':
if (_keyDownHandlerMap.containsKey(identifier)) {
_keyDownHandlerMap[identifier]!(hotKey);
}
break;
case 'onKeyUp':
if (_keyUpHandlerMap.containsKey(identifier)) {
_keyUpHandlerMap[identifier]!(hotKey);
}
break;
default:
UnimplementedError();
}
}
}
/// Handle in-app hot key event.
bool _handleInAppHotKeyEvent(KeyEvent keyEvent) {
if (_hotKeyList.where((e) => e.scope == HotKeyScope.inapp).isEmpty) {
return false;
}
if (keyEvent is KeyUpEvent && _lastPressedHotKey != null) {
HotKeyHandler? handler = _keyUpHandlerMap[_lastPressedHotKey!.identifier];
if (handler != null) handler(_lastPressedHotKey!);
_lastPressedHotKey = null;
return true;
}
if (keyEvent is KeyRepeatEvent && _lastPressedHotKey != null) {
return true;
}
if (keyEvent is KeyDownEvent) {
final physicalKeysPressed = HardwareKeyboard.instance.physicalKeysPressed;
HotKey? hotKey = _hotKeyList.firstWhereOrNull(
(e) {
List<HotKeyModifier>? modifiers = HotKeyModifier.values
.where((e) => e.physicalKeys.any(physicalKeysPressed.contains))
.toList();
return e.scope == HotKeyScope.inapp &&
keyEvent.logicalKey == e.logicalKey &&
modifiers.every((e.modifiers ?? []).contains);
},
);
if (hotKey != null) {
HotKeyHandler? handler = _keyDownHandlerMap[hotKey.identifier];
if (handler != null) handler(hotKey);
_lastPressedHotKey = hotKey;
return true;
}
}
return false;
}
List<HotKey> get registeredHotKeyList => _hotKeyList;
/// Register a hot key.
Future<void> register(
HotKey hotKey, {
HotKeyHandler? keyDownHandler,
HotKeyHandler? keyUpHandler,
}) async {
if (hotKey.scope == HotKeyScope.system) {
await _platform.register(hotKey);
}
if (keyDownHandler != null) {
_keyDownHandlerMap.update(
hotKey.identifier,
(_) => keyDownHandler,
ifAbsent: () => keyDownHandler,
);
}
if (keyUpHandler != null) {
_keyUpHandlerMap.update(
hotKey.identifier,
(_) => keyUpHandler,
ifAbsent: () => keyUpHandler,
);
}
_hotKeyList.add(hotKey);
}
/// Unregister a hot key.
Future<void> unregister(HotKey hotKey) async {
if (hotKey.scope == HotKeyScope.system) {
await _platform.unregister(hotKey);
}
if (_keyDownHandlerMap.containsKey(hotKey.identifier)) {
_keyDownHandlerMap.remove(hotKey.identifier);
}
if (_keyUpHandlerMap.containsKey(hotKey.identifier)) {
_keyUpHandlerMap.remove(hotKey.identifier);
}
_hotKeyList.removeWhere((e) => e.identifier == hotKey.identifier);
}
/// Unregister all hot keys.
Future<void> unregisterAll() async {
await _platform.unregisterAll();
_keyDownHandlerMap.clear();
_keyUpHandlerMap.clear();
_hotKeyList.clear();
}
}
final hotKeyManager = HotKeyManager.instance;