-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathkeyboard.js
95 lines (75 loc) · 2.51 KB
/
keyboard.js
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
// Keyboard shortcuts
export let activate_keyboard_shortcuts = () => {
console.log("Activating keyboard shortcuts.");
// Map variables representing keys to ASCII codes
let [ A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z ] = Array.from({ length: 26 }, (v, i) => 65 + i);
const Delete = 46;
const Shift = 16;
const Ctrl = 17;
const Alt = 18;
// Arrow keys
const Left = 37;
const Right = 39;
const Up = 38;
const Down = 40;
// Key is pressed
document.body.addEventListener("keydown", (e) => {
let key = e.which || e.keyCode;
if (key == X) {} // delete
if (key == C) {} // copy
if (key == V) {} // paste
if (key == A) {} // A
if (key == B) {} // B
/* ...etc... */
if (key == X) {
console.log("Delete currently selected text or element (for example.)");
window.drawing = false;
} // X
if (key == Z) {} // Z
// Arrows
if (key == Left) {}
if (key == Right) {}
if (key == Up) {}
if (key == Down) {}
if (key == Delete) { }
if (key == Shift) { window.keyShift = true; }
if (key == Ctrl) { window.keyCtrl = true; }
if (key == Alt) { window.keyAlt = true; }
// Shift + Delete
if (window.keyShift && key == Delete) {}
// Shift + Alt
if (window.keyShift && key == Alt) {}
// Ctrl + C
if (window.keyCtrl && key == C) {}
// Ctrl + V
if (window.keyCtrl && key == V) {}
});
// Key (is) depressed :...(
document.body.addEventListener("keyup", (e) => {
const key = e.which || e.keyCode;
if (key == X) {} // delete
if (key == C) {} // copy
if (key == V) {} // paste
if (key == A) {} // A
if (key == B) {} // B
/* ...etc... */
if (key == Z) {} // Z
// Arrows
if (key == Left) {}
if (key == Right) {}
if (key == Up) {}
if (key == Down) {}
if (key == Delete) { /* delete something currently selected */ }
if (key == Shift) { window.keyShift = false; }
if (key == Ctrl) { window.keyCtrl = false; }
if (key == Alt) { window.keyAlt = false; }
// Shift + Delete
if (window.keyShift && key == Delete) {}
// Shift + Alt
if (window.keyShift && key == Alt) {}
// Ctrl + C
if (window.keyCtrl && key == C) {}
// Ctrl + V
if (window.keyCtrl && key == V) {}
});
}