Repository navigation
Expand file tree
/
Copy pathscreenKbd.js
More file actions
102 lines (90 loc) · 2.92 KB
/
Copy pathscreenKbd.js
File metadata and controls
102 lines (90 loc) · 2.92 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
/*
#sh.. integrate this.
should keyrepeat functionality be here/in key ?
but we'd need to reuse repeat
should unify key handling, bool up down
and handle maximize down by flipping fractionscale + autoscale
*/
export const kbdWidth = 220, kbdHeight = 220;
const touchKeyMap = new Map(); // Map<touch.identifier, Element>
let handleKey = null;
function getKeyAtPoint(x, y) {
return document.elementFromPoint(x, y)?.closest('.key');
}
function activateKey(key) {
if (key && !key.classList.contains('active')) {
key.classList.add('active');
if (handleKey) {
handleKey(true, key.dataset.key);
}
}
}
function deactivateKey(key) {
if (key && key.classList.contains('active')) {
key.classList.remove('active');
if (handleKey) {
handleKey(false, key.dataset.key);
}
}
}
// Handle touch start
function handleTouchStart(e) {
e.preventDefault();
const touches = e.changedTouches;
for (let i = 0; i < touches.length; i++) {
const touch = touches[i];
const key = getKeyAtPoint(touch.clientX, touch.clientY);
if (key) {
touchKeyMap.set(touch.identifier, key);
activateKey(key);
}
}
}
// Handle touch move
function handleTouchMove(e) {
e.preventDefault();
const touches = e.changedTouches;
for (let i = 0; i < touches.length; i++) {
const touch = touches[i];
const currentKey = getKeyAtPoint(touch.clientX, touch.clientY);
const previousKey = touchKeyMap.get(touch.identifier);
// If the touch moved to a new key
if (currentKey !== previousKey) {
// Deactivate the previous key
deactivateKey(previousKey);
// Activate the new key
if (currentKey) {
activateKey(currentKey);
touchKeyMap.set(touch.identifier, currentKey);
} else {
touchKeyMap.delete(touch.identifier);
}
}
}
}
// Handle touch end or cancel
function handleTouchEnd(e) {
e.preventDefault();
const touches = e.changedTouches;
for (let i = 0; i < touches.length; i++) {
const touch = touches[i];
const key = touchKeyMap.get(touch.identifier);
if (key) {
deactivateKey(key);
touchKeyMap.delete(touch.identifier);
}
}
}
export function initKbdListeners() {
// Add touch event listeners to keypad areas
const keypads = document.querySelectorAll('.keypad-part');
keypads.forEach(keypad => {
keypad.addEventListener('touchstart', handleTouchStart, { passive: false });
keypad.addEventListener('touchmove', handleTouchMove, { passive: false });
keypad.addEventListener('touchend', handleTouchEnd, { passive: false });
keypad.addEventListener('touchcancel', handleTouchEnd, { passive: false });
});
}
export function setKbdHandler(handler) {
handleKey = handler;
}