@cldmv/node-android-tv-remote 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +8 -0
- package/.prettierrc +15 -0
- package/README.md +110 -0
- package/docs/KEYBOARD_KEYS.md +80 -0
- package/docs/KEYCODES.md +55 -0
- package/eslint.config.mjs +45 -0
- package/package.json +44 -0
- package/scripts/postinstall.js +48 -0
- package/src/data/keycodes.json +270 -0
- package/src/data/remote-keys.json +45 -0
- package/src/lib/android-tv-remote.js +549 -0
- package/test/android-tv-remote.test.js +73 -0
- package/test/connect-readall.js +102 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[
|
|
2
|
+
"home",
|
|
3
|
+
"back",
|
|
4
|
+
"menu",
|
|
5
|
+
"ok",
|
|
6
|
+
"select",
|
|
7
|
+
"up",
|
|
8
|
+
"down",
|
|
9
|
+
"left",
|
|
10
|
+
"right",
|
|
11
|
+
"play",
|
|
12
|
+
"pause",
|
|
13
|
+
"playPause",
|
|
14
|
+
"stop",
|
|
15
|
+
"next",
|
|
16
|
+
"previous",
|
|
17
|
+
"fastForward",
|
|
18
|
+
"rewind",
|
|
19
|
+
"volumeUp",
|
|
20
|
+
"volumeDown",
|
|
21
|
+
"volumeMute",
|
|
22
|
+
"channelUp",
|
|
23
|
+
"channelDown",
|
|
24
|
+
"info",
|
|
25
|
+
"guide",
|
|
26
|
+
"settings",
|
|
27
|
+
"apps",
|
|
28
|
+
"caption",
|
|
29
|
+
"bookmark",
|
|
30
|
+
"help",
|
|
31
|
+
"power",
|
|
32
|
+
"input",
|
|
33
|
+
"mute",
|
|
34
|
+
"search",
|
|
35
|
+
"number0",
|
|
36
|
+
"number1",
|
|
37
|
+
"number2",
|
|
38
|
+
"number3",
|
|
39
|
+
"number4",
|
|
40
|
+
"number5",
|
|
41
|
+
"number6",
|
|
42
|
+
"number7",
|
|
43
|
+
"number8",
|
|
44
|
+
"number9"
|
|
45
|
+
]
|
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Android TV Remote module.
|
|
3
|
+
* @module android-tv-remote
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} RemoteConfig
|
|
7
|
+
* @property {string} ip - The IP address of the device.
|
|
8
|
+
* @property {number} [port=5555] - The port for ADB connection.
|
|
9
|
+
* @property {string} [inputDevice="/dev/input/event0"] - The input device path.
|
|
10
|
+
* @property {boolean} [autoConnect=true] - Whether to auto-connect on command.
|
|
11
|
+
* @property {boolean} [autoDisconnect=true] - Whether to auto-disconnect after inactivity.
|
|
12
|
+
* @property {number} [disconnectTimeout=10] - Inactivity timeout in seconds before disconnecting.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {Object} Remote
|
|
16
|
+
* @property {function(string, boolean=): Promise<void>} handleSettings - Get or set Android settings via ADB.
|
|
17
|
+
* @property {function(function(Error=, any=)=): Promise|undefined} connect - Connect to the device. Supports both promise and callback styles.
|
|
18
|
+
* @property {function(function(Error=, any=)=): Promise|undefined} disconnect - Disconnect from the device. Supports both promise and callback styles.
|
|
19
|
+
* @property {function(number, function(Error=, any=)=): Promise|undefined} inputKeycode - Send a keycode to the device. Supports both promise and callback styles.
|
|
20
|
+
* @property {Object} press - Remote control key functions for Android TV remotes.
|
|
21
|
+
* @property {Object} keyboard - Keyboard interface for all keys, with text and keycode fallback.
|
|
22
|
+
* @property {Object} keyboard.key - Contains all key functions.
|
|
23
|
+
* @property {Object} keyboard.key.shift - Contains all shifted key functions.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const adb = require("adbkit");
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Logs a message with a datetime prefix.
|
|
30
|
+
* @private
|
|
31
|
+
* @param {...any} args - Arguments to log.
|
|
32
|
+
*/
|
|
33
|
+
function logWithTime(...args) {
|
|
34
|
+
const now = new Date().toISOString();
|
|
35
|
+
console.log(`[${now}]`, ...args);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Logs a warning with a datetime prefix.
|
|
40
|
+
* @private
|
|
41
|
+
* @param {...any} args - Arguments to warn.
|
|
42
|
+
*/
|
|
43
|
+
function warnWithTime(...args) {
|
|
44
|
+
const now = new Date().toISOString();
|
|
45
|
+
console.warn(`[${now}]`, ...args);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Logs an error with a datetime prefix.
|
|
50
|
+
* @private
|
|
51
|
+
* @param {...any} args - Arguments to error.
|
|
52
|
+
*/
|
|
53
|
+
function errorWithTime(...args) {
|
|
54
|
+
const now = new Date().toISOString();
|
|
55
|
+
console.error(`[${now}]`, ...args);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Wraps an async function to support both promise and callback styles.
|
|
60
|
+
* @internal
|
|
61
|
+
* @param {Function} fn - The async function to wrap.
|
|
62
|
+
* @returns {Function} A function supporting both promise and callback usage.
|
|
63
|
+
*/
|
|
64
|
+
function wrapAsync(fn) {
|
|
65
|
+
return function (...args) {
|
|
66
|
+
const cb = typeof args[args.length - 1] === "function" ? args.pop() : null;
|
|
67
|
+
const p = fn.apply(this, args);
|
|
68
|
+
if (cb) {
|
|
69
|
+
p.then((result) => cb(null, result)).catch((err) => cb(err));
|
|
70
|
+
} else {
|
|
71
|
+
return p;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Handles disconnect and connection errors, outputs helpful messages, and exits the process.
|
|
78
|
+
* @private
|
|
79
|
+
* @param {Error} err - The error object.
|
|
80
|
+
*/
|
|
81
|
+
function handleDisconnectError(err) {
|
|
82
|
+
errorWithTime("Error:", err.message || err);
|
|
83
|
+
if (err.message && err.message.includes("device unauthorized")) {
|
|
84
|
+
errorWithTime(
|
|
85
|
+
"Your device is unauthorized. Please check your TV and accept the authorization dialog to allow this system to connect via ADB."
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Android keycodes mapping loaded from JSON file.
|
|
93
|
+
* @internal
|
|
94
|
+
* @type {Object.<string, number>}
|
|
95
|
+
* @see https://developer.android.com/reference/android/view/KeyEvent
|
|
96
|
+
*/
|
|
97
|
+
const keycodes = require("../data/keycodes.json");
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Remote keys mapping loaded from JSON file.
|
|
101
|
+
* @internal
|
|
102
|
+
* @type {string[]}
|
|
103
|
+
*/
|
|
104
|
+
const remoteKeys = require("../data/remote-keys.json");
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Factory function to create a Remote instance.
|
|
108
|
+
* @function
|
|
109
|
+
* @public
|
|
110
|
+
* @param {RemoteConfig} config - Configuration for the remote.
|
|
111
|
+
* @returns {Remote}
|
|
112
|
+
*/
|
|
113
|
+
module.exports = function (config) {
|
|
114
|
+
/**
|
|
115
|
+
* @type {RemoteConfig}
|
|
116
|
+
*/
|
|
117
|
+
config = config || {};
|
|
118
|
+
const ip = config.ip;
|
|
119
|
+
const port = config.port || 5555;
|
|
120
|
+
const inputDevice = config.inputDevice || "/dev/input/event0";
|
|
121
|
+
const host = ip + ":" + port;
|
|
122
|
+
const client = adb.createClient();
|
|
123
|
+
let connected = false;
|
|
124
|
+
const autoConnect = config.autoConnect !== false; // default true
|
|
125
|
+
const autoDisconnect = config.autoDisconnect !== false; // default true
|
|
126
|
+
const disconnectTimeout = typeof config.disconnectTimeout === "number" ? config.disconnectTimeout : 10;
|
|
127
|
+
let disconnectTimer = null;
|
|
128
|
+
const quiet = config.quiet !== false; // default true
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Resets the disconnect timer if autoDisconnect is enabled.
|
|
132
|
+
* @private
|
|
133
|
+
*/
|
|
134
|
+
function resetDisconnectTimer() {
|
|
135
|
+
if (!autoDisconnect) return;
|
|
136
|
+
if (disconnectTimer) clearTimeout(disconnectTimer);
|
|
137
|
+
disconnectTimer = setTimeout(function () {
|
|
138
|
+
if (connected) disconnect();
|
|
139
|
+
}, disconnectTimeout * 1000);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Ensures connection if autoConnect is enabled.
|
|
144
|
+
* @internal
|
|
145
|
+
* @returns {Promise<void>}
|
|
146
|
+
*/
|
|
147
|
+
function ensureConnected() {
|
|
148
|
+
if (!autoConnect) return Promise.resolve();
|
|
149
|
+
if (connected) return Promise.resolve();
|
|
150
|
+
return connect();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Connects to the ADB device.
|
|
155
|
+
* @internal
|
|
156
|
+
* @returns {Promise<void>}
|
|
157
|
+
*/
|
|
158
|
+
function connect() {
|
|
159
|
+
return client
|
|
160
|
+
.connect(ip, port)
|
|
161
|
+
.then(function () {
|
|
162
|
+
connected = true;
|
|
163
|
+
if (!quiet) logWithTime("Connected to " + host);
|
|
164
|
+
})
|
|
165
|
+
.catch(function (err) {
|
|
166
|
+
if (err.message && err.message.includes("already connected")) {
|
|
167
|
+
if (!quiet) warnWithTime("Warning: Device already connected.");
|
|
168
|
+
connected = true;
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
handleDisconnectError(err);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
const connectWrapped = wrapAsync(connect);
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Disconnects from the ADB device.
|
|
178
|
+
* @internal
|
|
179
|
+
* @returns {Promise<void>}
|
|
180
|
+
*/
|
|
181
|
+
function disconnect() {
|
|
182
|
+
if (disconnectTimer) {
|
|
183
|
+
clearTimeout(disconnectTimer);
|
|
184
|
+
disconnectTimer = null;
|
|
185
|
+
}
|
|
186
|
+
return client
|
|
187
|
+
.disconnect(ip, port)
|
|
188
|
+
.then(function () {
|
|
189
|
+
connected = false;
|
|
190
|
+
if (!quiet) logWithTime("Disconnected from " + host);
|
|
191
|
+
})
|
|
192
|
+
.catch(function (err) {
|
|
193
|
+
if (err.message && err.message.includes("disconnected")) {
|
|
194
|
+
if (!quiet) warnWithTime("Warning: Device already disconnected before explicit disconnect call.");
|
|
195
|
+
connected = false;
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
handleDisconnectError(err);
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
const disconnectWrapped = wrapAsync(disconnect);
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Sends a keycode to the device, auto-connects/disconnects as needed.
|
|
205
|
+
* @internal
|
|
206
|
+
* @param {number} code - The Android keycode to send.
|
|
207
|
+
* @returns {Promise}
|
|
208
|
+
*/
|
|
209
|
+
function inputKeycode(code) {
|
|
210
|
+
return ensureConnected().then(function () {
|
|
211
|
+
resetDisconnectTimer();
|
|
212
|
+
return client.shell(host, "input keyevent " + code);
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
const inputKeycodeWrapped = wrapAsync(inputKeycode);
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Sends a long press keycode to the device, auto-connects/disconnects as needed.
|
|
219
|
+
* @internal
|
|
220
|
+
* @param {number} code - The Android keycode to send as a long press.
|
|
221
|
+
* @returns {Promise}
|
|
222
|
+
*/
|
|
223
|
+
function inputKeycodeLongPress(code) {
|
|
224
|
+
const cmd = "sendevent " + inputDevice + " 1 " + code + " 1 && " + "sleep 1 && " + "sendevent " + inputDevice + " 1 " + code + " 0";
|
|
225
|
+
return ensureConnected().then(function () {
|
|
226
|
+
resetDisconnectTimer();
|
|
227
|
+
return client.shell(host, cmd);
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
const inputKeycodeLongPressWrapped = wrapAsync(inputKeycodeLongPress);
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Sends text input to the device, auto-connects/disconnects as needed.
|
|
234
|
+
* @internal
|
|
235
|
+
* @param {string} text - The text to input.
|
|
236
|
+
* @returns {Promise}
|
|
237
|
+
*/
|
|
238
|
+
function inputText(text) {
|
|
239
|
+
const escaped = text.replace(/ /g, "%s");
|
|
240
|
+
return ensureConnected().then(function () {
|
|
241
|
+
resetDisconnectTimer();
|
|
242
|
+
return client.shell(host, 'input text "' + escaped + '"');
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
const inputTextWrapped = wrapAsync(inputText);
|
|
246
|
+
|
|
247
|
+
const keyboardKeys = {};
|
|
248
|
+
const keyboardShiftKeys = {};
|
|
249
|
+
|
|
250
|
+
for (let i = 0; i < 26; i++) {
|
|
251
|
+
const letter = String.fromCharCode(97 + i);
|
|
252
|
+
const code = 29 + i;
|
|
253
|
+
keyboardKeys[letter] = (function (code) {
|
|
254
|
+
return function () {
|
|
255
|
+
return inputKeycode(code);
|
|
256
|
+
};
|
|
257
|
+
})(code);
|
|
258
|
+
keyboardShiftKeys[letter] = (function (code) {
|
|
259
|
+
return function () {
|
|
260
|
+
return inputKeycode(keycodes.shiftLeft).then(function () {
|
|
261
|
+
return inputKeycode(code);
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
})(code);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
keyboardKeys.enter = function () {
|
|
268
|
+
return inputKeycode(keycodes.enter);
|
|
269
|
+
};
|
|
270
|
+
keyboardKeys.space = function () {
|
|
271
|
+
return inputKeycode(keycodes.space);
|
|
272
|
+
};
|
|
273
|
+
keyboardKeys.del = function () {
|
|
274
|
+
return inputKeycode(keycodes.del);
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
keyboardShiftKeys.enter = function () {
|
|
278
|
+
return inputKeycode(keycodes.shiftLeft).then(function () {
|
|
279
|
+
return inputKeycode(keycodes.enter);
|
|
280
|
+
});
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
return {
|
|
284
|
+
/**
|
|
285
|
+
* Get or set Android settings via ADB.
|
|
286
|
+
* @public
|
|
287
|
+
* @param {string} mode - 'get' or 'set'.
|
|
288
|
+
* @param {boolean} [overrideQuiet] - Optionally override the quiet flag for this call.
|
|
289
|
+
* @returns {Promise<void>}
|
|
290
|
+
* @example
|
|
291
|
+
* remote.handleSettings('get', false);
|
|
292
|
+
*/
|
|
293
|
+
handleSettings: function (mode, overrideQuiet) {
|
|
294
|
+
const keys = [
|
|
295
|
+
{ ns: "system", key: "screen_off_timeout", value: 2147483647 },
|
|
296
|
+
{ ns: "secure", key: "sleep_timeout", value: 0 },
|
|
297
|
+
{ ns: "global", key: "stay_on_while_plugged_in", value: 3 }
|
|
298
|
+
];
|
|
299
|
+
const useQuiet = typeof overrideQuiet === "boolean" ? overrideQuiet : quiet;
|
|
300
|
+
return client
|
|
301
|
+
.connect(ip, port)
|
|
302
|
+
.then(() => {
|
|
303
|
+
if (!useQuiet) logWithTime(`Connected to ${host}`);
|
|
304
|
+
let chain = Promise.resolve();
|
|
305
|
+
keys.forEach((item) => {
|
|
306
|
+
chain = chain.then(() => {
|
|
307
|
+
let cmd;
|
|
308
|
+
if (mode === "set") {
|
|
309
|
+
cmd = `settings put ${item.ns} ${item.key} ${item.value}`;
|
|
310
|
+
} else {
|
|
311
|
+
cmd = `settings get ${item.ns} ${item.key}`;
|
|
312
|
+
}
|
|
313
|
+
return client
|
|
314
|
+
.shell(host, cmd)
|
|
315
|
+
.then(adb.util.readAll)
|
|
316
|
+
.then((result) => {
|
|
317
|
+
if (!useQuiet) {
|
|
318
|
+
if (mode === "set") {
|
|
319
|
+
logWithTime(`Set ${item.ns} ${item.key} to ${item.value}`);
|
|
320
|
+
} else {
|
|
321
|
+
logWithTime(`${item.ns} ${item.key}: ${result.toString().trim()}`);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
return chain;
|
|
328
|
+
})
|
|
329
|
+
.then(() => client.disconnect(ip, port))
|
|
330
|
+
.then(() => {
|
|
331
|
+
if (!useQuiet) logWithTime("Disconnected cleanly");
|
|
332
|
+
})
|
|
333
|
+
.catch((err) => {
|
|
334
|
+
if (err.message && err.message.includes("disconnected")) {
|
|
335
|
+
if (!useQuiet) warnWithTime("Warning: Device already disconnected before explicit disconnect call.");
|
|
336
|
+
} else {
|
|
337
|
+
handleDisconnectError(err);
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
},
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Connect to the device. Supports both promise and callback styles.
|
|
344
|
+
* @public
|
|
345
|
+
* @param {function(Error=, any=)=} [cb]
|
|
346
|
+
* @returns {Promise|undefined}
|
|
347
|
+
*/
|
|
348
|
+
connect: connectWrapped,
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Disconnect from the device. Supports both promise and callback styles.
|
|
352
|
+
* @public
|
|
353
|
+
* @param {function(Error=, any=)=} [cb]
|
|
354
|
+
* @returns {Promise|undefined}
|
|
355
|
+
*/
|
|
356
|
+
disconnect: disconnectWrapped,
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Send a keycode. Supports both promise and callback styles.
|
|
360
|
+
* @public
|
|
361
|
+
* @param {number} code
|
|
362
|
+
* @param {function(Error=, any=)=} [cb]
|
|
363
|
+
* @returns {Promise|undefined}
|
|
364
|
+
*/
|
|
365
|
+
inputKeycode: inputKeycodeWrapped,
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* All remote key functions support both promise and callback styles.
|
|
369
|
+
* @public
|
|
370
|
+
*/
|
|
371
|
+
press: (() => {
|
|
372
|
+
/**
|
|
373
|
+
* Dynamically generated remote control key functions for Android TV remotes.
|
|
374
|
+
* Keys are loaded from remote-keys.json. Color buttons are excluded.
|
|
375
|
+
* @namespace press
|
|
376
|
+
*/
|
|
377
|
+
const obj = {};
|
|
378
|
+
const longObj = {};
|
|
379
|
+
remoteKeys.forEach(function (key) {
|
|
380
|
+
/**
|
|
381
|
+
* Sends the corresponding keycode for this remote key.
|
|
382
|
+
* @returns {Promise}
|
|
383
|
+
* @example
|
|
384
|
+
* press.home();
|
|
385
|
+
*/
|
|
386
|
+
obj[key] = wrapAsync(function () {
|
|
387
|
+
// Special handling for ok/select
|
|
388
|
+
if (key === "ok" || key === "select") {
|
|
389
|
+
return inputKeycode(keycodes.dpadCenter || keycodes.ok);
|
|
390
|
+
}
|
|
391
|
+
// Special handling for play/pause
|
|
392
|
+
if (key === "play") {
|
|
393
|
+
return inputKeycode(keycodes.mediaPlay || keycodes.playPause);
|
|
394
|
+
}
|
|
395
|
+
if (key === "pause") {
|
|
396
|
+
return inputKeycode(keycodes.mediaPause || keycodes.playPause);
|
|
397
|
+
}
|
|
398
|
+
if (key === "volumeMute" || key === "mute") {
|
|
399
|
+
return inputKeycode(keycodes.volumeMute || keycodes.mute);
|
|
400
|
+
}
|
|
401
|
+
if (key === "input") {
|
|
402
|
+
return inputKeycode(keycodes.tvInput);
|
|
403
|
+
}
|
|
404
|
+
if (key.startsWith("number")) {
|
|
405
|
+
// e.g. number0
|
|
406
|
+
const num = key.replace("number", "");
|
|
407
|
+
return inputKeycode(keycodes[num]);
|
|
408
|
+
}
|
|
409
|
+
return inputKeycode(keycodes[key]);
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Sends the corresponding keycode for this remote key as a long press.
|
|
414
|
+
* @returns {Promise}
|
|
415
|
+
* @example
|
|
416
|
+
* press.long.home();
|
|
417
|
+
*/
|
|
418
|
+
longObj[key] = wrapAsync(function () {
|
|
419
|
+
if (key === "ok" || key === "select") {
|
|
420
|
+
return inputKeycodeLongPressWrapped(keycodes.dpadCenter || keycodes.ok);
|
|
421
|
+
}
|
|
422
|
+
if (key === "play") {
|
|
423
|
+
return inputKeycodeLongPressWrapped(keycodes.mediaPlay || keycodes.playPause);
|
|
424
|
+
}
|
|
425
|
+
if (key === "pause") {
|
|
426
|
+
return inputKeycodeLongPressWrapped(keycodes.mediaPause || keycodes.playPause);
|
|
427
|
+
}
|
|
428
|
+
if (key === "volumeMute" || key === "mute") {
|
|
429
|
+
return inputKeycodeLongPressWrapped(keycodes.volumeMute || keycodes.mute);
|
|
430
|
+
}
|
|
431
|
+
if (key === "input") {
|
|
432
|
+
return inputKeycodeLongPressWrapped(keycodes.tvInput);
|
|
433
|
+
}
|
|
434
|
+
if (key.startsWith("number")) {
|
|
435
|
+
const num = key.replace("number", "");
|
|
436
|
+
return inputKeycodeLongPressWrapped(keycodes[num]);
|
|
437
|
+
}
|
|
438
|
+
return inputKeycodeLongPressWrapped(keycodes[key]);
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
obj.long = longObj;
|
|
442
|
+
return obj;
|
|
443
|
+
})(),
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Keyboard interface for all keys, with text and keycode fallback.
|
|
447
|
+
* @public
|
|
448
|
+
*/
|
|
449
|
+
keyboard: {
|
|
450
|
+
/**
|
|
451
|
+
* Sends text input to the device.
|
|
452
|
+
* @public
|
|
453
|
+
* @param {string} text - The text to input.
|
|
454
|
+
* @returns {Promise}
|
|
455
|
+
* @example
|
|
456
|
+
* keyboard.text('hello');
|
|
457
|
+
*/
|
|
458
|
+
text: inputTextWrapped,
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Key subobject: callable and contains all key functions.
|
|
462
|
+
*/
|
|
463
|
+
key: Object.assign(
|
|
464
|
+
wrapAsync(function (keyName, options) {
|
|
465
|
+
options = options || {};
|
|
466
|
+
if (!keycodes[keyName]) {
|
|
467
|
+
return Promise.reject(new Error("Unknown key: " + keyName));
|
|
468
|
+
}
|
|
469
|
+
if (!options.forceKeycode) {
|
|
470
|
+
// Try inputText for single character keys
|
|
471
|
+
if (keyName.length === 1) {
|
|
472
|
+
return inputText(keyName);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
// Fallback to keycode
|
|
476
|
+
return inputKeycode(keycodes[keyName]);
|
|
477
|
+
}),
|
|
478
|
+
{
|
|
479
|
+
// Dynamically add all key functions and .keycode subobject
|
|
480
|
+
...(() => {
|
|
481
|
+
const keyFns = {};
|
|
482
|
+
Object.keys(keycodes).forEach(function (keyName) {
|
|
483
|
+
/**
|
|
484
|
+
* Sends this key using inputText (if possible) or keycode fallback.
|
|
485
|
+
* @returns {Promise}
|
|
486
|
+
* @example
|
|
487
|
+
* keyboard.key.a();
|
|
488
|
+
*/
|
|
489
|
+
keyFns[keyName] = wrapAsync(function (options) {
|
|
490
|
+
options = options || {};
|
|
491
|
+
if (!options.forceKeycode && keyName.length === 1) {
|
|
492
|
+
return inputText(keyName);
|
|
493
|
+
}
|
|
494
|
+
return inputKeycode(keycodes[keyName]);
|
|
495
|
+
});
|
|
496
|
+
/**
|
|
497
|
+
* Sends this key using keycode only.
|
|
498
|
+
* @returns {Promise}
|
|
499
|
+
* @example
|
|
500
|
+
* keyboard.key.a.keycode();
|
|
501
|
+
*/
|
|
502
|
+
keyFns[keyName].keycode = wrapAsync(function () {
|
|
503
|
+
return inputKeycode(keycodes[keyName]);
|
|
504
|
+
});
|
|
505
|
+
});
|
|
506
|
+
return keyFns;
|
|
507
|
+
})(),
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Shift subobject for shifted keys.
|
|
511
|
+
*/
|
|
512
|
+
shift: (() => {
|
|
513
|
+
const shiftFns = {};
|
|
514
|
+
Object.keys(keycodes).forEach(function (keyName) {
|
|
515
|
+
/**
|
|
516
|
+
* Sends this key with shift using inputText (if possible) or keycode fallback.
|
|
517
|
+
* @returns {Promise}
|
|
518
|
+
* @example
|
|
519
|
+
* keyboard.key.shift.a();
|
|
520
|
+
*/
|
|
521
|
+
shiftFns[keyName] = wrapAsync(function (options) {
|
|
522
|
+
options = options || {};
|
|
523
|
+
if (!options.forceKeycode && keyName.length === 1) {
|
|
524
|
+
return inputText(keyName.toUpperCase());
|
|
525
|
+
}
|
|
526
|
+
return inputKeycodeWrapped(keycodes.shiftLeft).then(function () {
|
|
527
|
+
return inputKeycodeLongPressWrapped(keycodes[keyName]);
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Sends this key with shift using keycode only.
|
|
533
|
+
* @returns {Promise}
|
|
534
|
+
* @example
|
|
535
|
+
* keyboard.key.shift.a.keycode();
|
|
536
|
+
*/
|
|
537
|
+
shiftFns[keyName].keycode = wrapAsync(function () {
|
|
538
|
+
return inputKeycodeWrapped(keycodes.shiftLeft).then(function () {
|
|
539
|
+
return inputKeycodeLongPressWrapped(keycodes[keyName]);
|
|
540
|
+
});
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
return shiftFns;
|
|
544
|
+
})()
|
|
545
|
+
}
|
|
546
|
+
)
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
const Remote = require("../src/lib/android-tv-remote");
|
|
2
|
+
|
|
3
|
+
// Usage: Set ANDROID_TV_IP and ANDROID_TV_PORT env vars before running Jest
|
|
4
|
+
const TEST_IP = process.env.ANDROID_TV_IP || "192.168.1.100";
|
|
5
|
+
const TEST_PORT = process.env.ANDROID_TV_PORT ? parseInt(process.env.ANDROID_TV_PORT, 10) : 5555;
|
|
6
|
+
|
|
7
|
+
const config = {
|
|
8
|
+
ip: TEST_IP,
|
|
9
|
+
port: TEST_PORT,
|
|
10
|
+
quiet: true,
|
|
11
|
+
autoConnect: true,
|
|
12
|
+
autoDisconnect: true,
|
|
13
|
+
disconnectTimeout: 5
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const remote = Remote(config);
|
|
17
|
+
|
|
18
|
+
describe("android-tv-remote", () => {
|
|
19
|
+
jest.setTimeout(20000); // ADB/network can be slow
|
|
20
|
+
|
|
21
|
+
test("should connect and disconnect without error (promise)", async () => {
|
|
22
|
+
await remote.connect();
|
|
23
|
+
await remote.disconnect();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("should connect and disconnect without error (callback)", (done) => {
|
|
27
|
+
remote.connect((err) => {
|
|
28
|
+
expect(err).toBeFalsy();
|
|
29
|
+
remote.disconnect((err2) => {
|
|
30
|
+
expect(err2).toBeFalsy();
|
|
31
|
+
done();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("should send a keycode (HOME)", async () => {
|
|
37
|
+
await remote.connect();
|
|
38
|
+
await remote.inputKeycode(3); // 3 = KEYCODE_HOME
|
|
39
|
+
await remote.disconnect();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("should send text input", async () => {
|
|
43
|
+
await remote.connect();
|
|
44
|
+
await remote.keyboard.text("test");
|
|
45
|
+
await remote.disconnect();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("should call press.home()", async () => {
|
|
49
|
+
await remote.connect();
|
|
50
|
+
await remote.press.home();
|
|
51
|
+
await remote.disconnect();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("should call keyboard.key.a()", async () => {
|
|
55
|
+
await remote.connect();
|
|
56
|
+
await remote.keyboard.key.a();
|
|
57
|
+
await remote.disconnect();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("should call keyboard.key.shift.a()", async () => {
|
|
61
|
+
await remote.connect();
|
|
62
|
+
await remote.keyboard.key.shift.a();
|
|
63
|
+
await remote.disconnect();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("should handle settings get", async () => {
|
|
67
|
+
await remote.handleSettings("get", true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("should handle settings set", async () => {
|
|
71
|
+
await remote.handleSettings("set", true);
|
|
72
|
+
});
|
|
73
|
+
});
|