-
Notifications
You must be signed in to change notification settings - Fork 0
/
testkb.html
132 lines (113 loc) · 3.78 KB
/
testkb.html
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
131
132
<!DOCTYPE html>
<html>
<body>
<button id="connect">CONNECT</button>
</body>
<script>
const SimpleMouseLinkUUID = '56beb2d8-64eb-4e33-96d4-e3f394041d0b';
const SimpleMouseMoveXYUUID = '83986548-8703-4272-a124-84abb9d03217';
const SimpleMouseButtonsUUID = '5062c9c1-ca09-47f9-84f6-725ef8091bf9';
const writeFuncs = {};
const fetchWriteCharacteristics = async (server) => {
const service = await server.getPrimaryService(SimpleMouseLinkUUID);
writeFuncs.move_xy = await service.getCharacteristic(SimpleMouseMoveXYUUID);
writeFuncs.buttons = await service.getCharacteristic(SimpleMouseButtonsUUID);
};
const trans_val = (val) => {
let res = Math.max(-127, Math.min(127, Math.round(val)));
return res
};
const move = (x, y) => {
if (writeFuncs.move_xy) {
let xx = trans_val(x);
let yy = trans_val(y);
console.log("Try to move ", xx, yy)
if (xx || yy) writeFuncs.move_xy.writeValue(new Uint8Array([xx, yy]));
}
};
const buttons = (left, right, middle) => {
const btn = (left ? 1<<0 : 0) + (right ? 1<<1 : 0) + (middle ? 1<<2 : 0);
if (writeFuncs.buttons) {
console.log("Mouse buttons:", left, right, middle)
writeFuncs.buttons.writeValue(new Uint8Array([btn]));
}
}
const openDevice = async (device) => {
const server = await device.gatt.connect();
try {
await fetchWriteCharacteristics(server);
console.log('connected', device);
} catch (err) {
console.warn(err);
}
};
/**
* A simple feedback from the keyboard to control the simple mouse link
* ASDW = movement (20px)
* 123 = mouse buttons (left, middle, right)
*/
const setupKeyHandlers = () => {
const btn = {
left: false,
right: false,
middle: false
};
const handleKButtons = (keycode, isdown) => {
let btnChange = true;
switch(keycode) {
case 'Digit1':
btn.left = isdown;
break;
case 'Digit2':
btn.middle = isdown;
break;
case 'Digit3':
btn.right = isdown;
break;
default:
btnChange = false;
}
if (btnChange) buttons(btn.left, btn.right, btn.middle);
}
const handleKMove = (keycode) => {
switch(keycode) {
case 'KeyA':
move(-20, 0);
break;
case 'KeyW':
move(0, -20);
break;
case 'KeyS':
move(0, 20);
break;
case 'KeyD':
move(20, 0);
break;
}
}
window.addEventListener('keydown', e => {
if(e.repeat) return; // Discard automatic repeat
handleKButtons(e.code, true);
handleKMove(e.code);
});
window.addEventListener('keyup', e => {
if(e.repeat) return; // Discard automatic repeat
handleKButtons(e.code, false);
});
}
const init = async () => {
const connectBtn = document.querySelector("#connect");
connectBtn.addEventListener('click', async () => {
const device = await navigator.bluetooth.requestDevice({
filters: [
{ services: [SimpleMouseLinkUUID] },
{ name: 'Simple Mouse Link'}
]
});
await openDevice(device);
});
setupKeyHandlers();
};
window.addEventListener('load', init);
</script>
</html>