-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-keypress.js
75 lines (66 loc) · 2.26 KB
/
test-keypress.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
const keypress = require('keypress');
const adb = require('adbkit');
const client = adb.createClient();
// Enable keyboard input
keypress(process.stdin);
let deviceId = undefined;
// Function to execute ADB shell commands
async function executeADBShellCommand(command) {
if (!deviceId) {
const device = await client.listDevices();
if (!device || device.length === 0) {
console.error('No connected Android device found.');
return;
}
deviceId = device[0].id;
}
client.shell(deviceId, command);
}
// Function to send key events to ADB
let firstrun = true
async function sendKeyEvent(keyCode) {
const command = `input keyevent ${keyCode}`;
if (firstrun) {
await executeADBShellCommand(command);
firstrun = false
} else executeADBShellCommand(command);
}
// Function to process keyboard input
function processKeyboardInput(ch, key) {
if (key) {
switch (key.name) {
case 'up':
sendKeyEvent('KEYCODE_DPAD_UP');
break;
case 'down':
sendKeyEvent('KEYCODE_DPAD_DOWN');
break;
case 'left':
sendKeyEvent('KEYCODE_DPAD_LEFT');
break;
case 'right':
sendKeyEvent('KEYCODE_DPAD_RIGHT');
break;
case 'return':
sendKeyEvent('KEYCODE_ENTER');
break;
case 'backspace':
sendKeyEvent('KEYCODE_BACK');
break;
case 'space':
sendKeyEvent('KEYCODE_HOME');
break;
case 'escape':
process.stdin.pause();
break;
default:
console.log('Invalid input! Use arrow keys, press "Enter" to send Enter key, "Backspace" for Android Back, "Space" for Android Home, or press "Escape" to quit.');
break;
}
}
}
// Start listening to keyboard input
console.log('Use arrow keys for Up and Down, Left and Right, press "Enter" to send Enter key, "Backspace" for Android Back, "Space" for Android Home, or press "Escape" to quit.');
process.stdin.on('keypress', processKeyboardInput);
process.stdin.setRawMode(true);
process.stdin.resume();