From ae63aaf1f4d3e62bcb9542ed4ac9a9690793096e Mon Sep 17 00:00:00 2001 From: Alexey Strokin Date: Tue, 11 Feb 2025 15:36:47 +0200 Subject: [PATCH] add send text functionality --- README.md | 7 +++++++ example/example.js | 6 ++++++ example/package.json | 2 +- package.json | 2 +- src/index.js | 4 ++++ src/remote/RemoteManager.js | 18 ++++++++++++++++++ src/remote/RemoteMessageManager.js | 17 +++++++++++++++++ src/remote/remotemessage.proto | 11 +++++++++-- 8 files changed, 63 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fe581f6..66e79b9 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ androidRemote.on('ready', async () => { androidRemote.sendKey(RemoteKeyCode.MUTE, RemoteDirection.SHORT) + androidRemote.sendText("Hello World!") + androidRemote.sendAppLink("https://www.disneyplus.com"); }); @@ -82,6 +84,11 @@ Emitted when androidtv has a problem : by example when you send a wrong app_link ### `Command: sendCode(code)` - `code` : You need to pass the shown code on the TV when asked +### `Command: sendText(text)` +- `text` : You need to pass the text to send + * This may not work when virtual keyboard is open on TV + * To hide virtual keyboard, send `sendKey(RemoteKeyCode.KEYCODE_BACK, RemoteDirection.SHORT)` + ### `Command: sendKey(KeyCode, Direction)` - `KeyCode` : Any key of https://developer.android.com/reference/android/view/KeyEvent?hl=fr - `Direction` : diff --git a/example/example.js b/example/example.js index 99ffbd3..c74eec4 100644 --- a/example/example.js +++ b/example/example.js @@ -55,6 +55,12 @@ androidRemote.on('ready', async () => { await new Promise(resolve => setTimeout(resolve, 100)); androidRemote.sendKey(RemoteKeyCode.KEYCODE_0, RemoteDirection.END_LONG) + await new Promise(resolve => setTimeout(resolve, 1000)); + androidRemote.sendText("Hello"); + + await new Promise(resolve => setTimeout(resolve, 1000)); + androidRemote.sendText(" World!"); + androidRemote.sendKey(RemoteKeyCode.KEYCODE_MUTE, RemoteDirection.SHORT) await new Promise(resolve => setTimeout(resolve, 10000)); androidRemote.sendAppLink("https://www.disneyplus.com"); diff --git a/example/package.json b/example/package.json index 297f4a3..b5a6657 100644 --- a/example/package.json +++ b/example/package.json @@ -10,6 +10,6 @@ "license": "ISC", "type": "module", "dependencies": { - "androidtv-remote": "^1.0.10" + "androidtv-remote": "file:../" } } diff --git a/package.json b/package.json index d17e424..4af19e3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "androidtv-remote", - "version": "1.0.10", + "version": "1.1.0", "description": "AndroidTV Remote", "main": "dist/index.js", "engines": { diff --git a/src/index.js b/src/index.js index 8d2c183..12f1689 100644 --- a/src/index.js +++ b/src/index.js @@ -79,6 +79,10 @@ export class AndroidRemote extends EventEmitter { return this.remoteManager.sendKey(key, direction); } + sendText(text){ + return this.remoteManager.sendText(text); + } + getCertificate(){ return { key:this.cert.key, diff --git a/src/remote/RemoteManager.js b/src/remote/RemoteManager.js index 844fd22..dca0f5c 100644 --- a/src/remote/RemoteManager.js +++ b/src/remote/RemoteManager.js @@ -75,6 +75,7 @@ class RemoteManager extends EventEmitter { } else if(message.remoteImeBatchEdit){ console.debug("Receive IME BATCH EDIT" + message.remoteImeBatchEdit); + this.remoteImeBatchEditResponse = JSON.parse(JSON.stringify(message.remoteImeBatchEdit)); } else if(message.remoteImeShowRequest){ console.debug("Receive IME SHOW REQUEST" + message.remoteImeShowRequest); @@ -167,6 +168,23 @@ class RemoteManager extends EventEmitter { key)); } + sendText(text){ + let _imeCounter = 0; + let _fieldCounter = 0; + if (this.remoteImeBatchEditResponse) { + if (this.remoteImeBatchEditResponse.imeCounter) { + _imeCounter = this.remoteImeBatchEditResponse.imeCounter; + } + if (this.remoteImeBatchEditResponse.fieldCounter) { + _fieldCounter = this.remoteImeBatchEditResponse.fieldCounter; + } + } + this.client.write(remoteMessageManager.createRemoteImeBatchEdit( + text, + _imeCounter, + _fieldCounter)); + } + sendAppLink(app_link){ this.client.write(remoteMessageManager.createRemoteRemoteAppLinkLaunchRequest(app_link)); } diff --git a/src/remote/RemoteMessageManager.js b/src/remote/RemoteMessageManager.js index d4e6761..b01ea86 100644 --- a/src/remote/RemoteMessageManager.js +++ b/src/remote/RemoteMessageManager.js @@ -112,6 +112,23 @@ class RemoteMessageManager { }); } + createRemoteImeBatchEdit(text, imeCounter, fieldCounter) { + return this.create({ + remoteImeBatchEdit: { + imeCounter, + fieldCounter, + editInfo: [{ + insert: 0, + textFieldStatus: { + start: text.length - 1, + end: text.length - 1, + value: text + } + }] + } + }); + } + parse(buffer){ return this.RemoteMessage.decodeDelimited(buffer); } diff --git a/src/remote/remotemessage.proto b/src/remote/remotemessage.proto index f3a40aa..c4e67ff 100644 --- a/src/remote/remotemessage.proto +++ b/src/remote/remotemessage.proto @@ -59,13 +59,20 @@ message RemoteImeShowRequest { } message RemoteEditInfo { - int32 insert = 2; + int32 insert = 1; + RemoteImeObject text_field_status = 2; +} + +message RemoteImeObject { + int32 start = 1; + int32 end = 2; + string value = 3; } message RemoteImeBatchEdit { int32 ime_counter = 1; int32 field_counter = 2; - RemoteEditInfo edit_info = 3; + repeated RemoteEditInfo edit_info = 3; } message RemoteAppInfo {