-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinputManager.js
More file actions
130 lines (114 loc) · 3.08 KB
/
Copy pathinputManager.js
File metadata and controls
130 lines (114 loc) · 3.08 KB
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
import {AppManager} from "./appManager.js";
/**
* @class InputManager
* @description Manages the input of the user
*/
export class InputManager {
/**
* The instance of the InputManager
* @type {InputManager}
*/
static instance = null;
constructor() {
if (InputManager.instance === null) {
InputManager.instance = this;
}
// this.disableContextMenu();
this.catchCtrlC(() => {
console.log("Ctrl+C");
AppManager.instance.stopForegroundApp().then(r => {
});
})
return InputManager.instance;
}
/**
* Disable the context menu
*/
disableContextMenu() {
window.addEventListener('contextmenu', (event) => {
event.preventDefault();
});
}
/**
* catch strg + c
* @param {function} callback - The callback function
*/
catchCtrlC(callback) {
window.addEventListener('keydown', (event) => {
if (event.key === 'c' && event.ctrlKey) {
callback();
}
});
}
/**
* Wait for specific key press
* @param {string} key - The key to wait for
*/
async waitFor(key = "") {
return new Promise((resolve) => {
const callback = (event) => {
if (event.key === key || key === "") {
window.removeEventListener('keydown', callback);
//prevent default
event.preventDefault();
resolve();
}
};
window.addEventListener('keydown', callback);
});
}
/**
* Add a key Down listener
* @param {string} key - The key to listen for
*/
waitKeyRaw(key) {
return new Promise((resolve) => {
const callback = (event) => {
if (event.key === key) {
event.preventDefault();
resolve();
}
};
window.addEventListener('keydown', callback);
});
}
/**
* Add a key Down listener
* @param key
* @param callback
*/
onKeyDown(key, callback) {
const callback2 = (event) => {
if (event.key === key) {
event.preventDefault();
callback(event);
}
};
window.addEventListener('keydown', callback2);
}
/**
* Remove a event listener
* @param key {string} - The key to remove
* @param callback {function} - The callback function
*/
removeKeyDown(key, callback) {
window.removeEventListener(key, callback);
}
/**
* focus the input on left click
* @param {HTMLInputElement} input - The input to focus
*/
focusInput(input) {
window.addEventListener('click', () => {
input.focus();
});
}
/**
* unFocus the input on left click
* @param {HTMLInputElement} input - The input to unfocus
*/
unFocusInput(input) {
window.removeEventListener('click', () => {
});
}
}