-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyboard_handler.js
31 lines (26 loc) · 983 Bytes
/
keyboard_handler.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
async function send_keyboard(url){
const response = await fetch(url);
const response2 = await response.text();
let parser = new DOMParser();
doc = parser.parseFromString( response2, 'text/html' );
document.replaceChild( doc.documentElement, document.documentElement );
}
document.addEventListener('keypress', (e) => {
const activeElement = document.activeElement;
//console.log(e.key,e.code,e.keyCode)
//console.log(activeElement.tagName)
if ("body" == activeElement.tagName.toLowerCase()){
//only if InputElement dont have focus
send_keyboard("/keyboard/down/"+e.keyCode)
}
//would be better to check if document.activeElement.data-attribute["keyboard"]
//so that each elements can have separate event
});
document.addEventListener('keydown', (e) => {
const activeElement = document.activeElement;
if ("body" == activeElement.tagName.toLowerCase()){
if (e.key.length > 1){
send_keyboard("/keyboard/down/keydown-"+e.key)
}
}
});