aboutsummaryrefslogtreecommitdiffstats
path: root/src/multi_key_command.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/multi_key_command.ts')
-rw-r--r--src/multi_key_command.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/multi_key_command.ts b/src/multi_key_command.ts
new file mode 100644
index 0000000..7e303fe
--- /dev/null
+++ b/src/multi_key_command.ts
@@ -0,0 +1,29 @@
+import key_codes, { KeyCode } from './key_codes';
+
+export default function(
+ el: HTMLDocument | HTMLElement,
+ command: KeyCode[],
+ action: () => void
+): void {
+ var key_buffer: number[] = [];
+
+ el.addEventListener('keydown', function(e: KeyboardEvent) {
+ command.forEach((key_code) => {
+ if (e.keyCode === key_code) {
+ key_buffer.push(key_code);
+ }
+ });
+ });
+
+ el.addEventListener('keyup', function(e: KeyboardEvent) {
+ if (key_buffer.length >= 2) {
+ var command_executed = key_buffer.length === command.length
+ && key_buffer.every((value, index) => value === command[index]);
+
+ if (command_executed) {
+ action();
+ key_buffer = [];
+ }
+ }
+ });
+};