From 4449677e360600a6711bce453162a1be5707088f Mon Sep 17 00:00:00 2001 From: antelle Date: Sat, 30 Jan 2021 21:34:08 +0100 Subject: [PATCH] entering text as key codes --- app/scripts/auto-type/auto-type-emitter.js | 18 ++++++++---- app/scripts/comp/launcher/native-modules.js | 14 ++++----- desktop/native-module-host.js | 32 ++++++++++++++++++--- package-lock.json | 4 +-- package.json | 2 +- 5 files changed, 50 insertions(+), 20 deletions(-) diff --git a/app/scripts/auto-type/auto-type-emitter.js b/app/scripts/auto-type/auto-type-emitter.js index 41831ec1..a0348966 100644 --- a/app/scripts/auto-type/auto-type-emitter.js +++ b/app/scripts/auto-type/auto-type-emitter.js @@ -74,11 +74,17 @@ class AutoTypeEmitter { } setMod(mod, enabled) { - // TODO: press the modifier + const nativeMod = ModMap[mod]; + if (!nativeMod) { + return this.callback(`Bad modifier: ${mod}`); + } + NativeModules.kbdKeyMoveWithModifier(!!enabled, [nativeMod]).catch((e) => { + logger.error('Error moving modifier', mod, enabled ? 'down' : 'up', e); + }); if (enabled) { - this.mod[ModMap[mod]] = true; + this.mod[nativeMod] = true; } else { - delete this.mod[ModMap[mod]]; + delete this.mod[nativeMod]; } } @@ -86,9 +92,9 @@ class AutoTypeEmitter { if (!str) { return this.withCallback(Promise.resolve()); } - const mod = Object.keys(this.mod); - if (mod.length) { - // TODO + const mods = Object.keys(this.mod); + if (mods.length) { + this.withCallback(NativeModules.kbdTextAsKeys(str, mods)); } else { this.withCallback(NativeModules.kbdText(str)); } diff --git a/app/scripts/comp/launcher/native-modules.js b/app/scripts/comp/launcher/native-modules.js index 1939c680..ad8a357f 100644 --- a/app/scripts/comp/launcher/native-modules.js +++ b/app/scripts/comp/launcher/native-modules.js @@ -230,6 +230,10 @@ if (Launcher) { return this.call('kbdText', str); }, + kbdTextAsKeys(str, mods) { + return this.call('kbdTextAsKeys', str, mods); + }, + kbdKeyPress(code, modifiers) { return this.call('kbdKeyPress', code, modifiers); }, @@ -238,13 +242,9 @@ if (Launcher) { return this.call('kbdShortcut', code, modifiers); }, - // kbdKeyMoveWithCode(down, code, modifiers) { - // return this.call('kbdKeyMoveWithCode', down, code, modifiers); - // }, - // - // kbdKeyMoveWithModifier(down, modifiers) { - // return this.call('kbdKeyMoveWithModifier', down, modifiers); - // }, + kbdKeyMoveWithModifier(down, modifiers) { + return this.call('kbdKeyMoveWithModifier', down, modifiers); + }, kbdKeyPressWithCharacter(character, code, modifiers) { return this.call('kbdKeyPressWithCharacter', character, code, modifiers); diff --git a/desktop/native-module-host.js b/desktop/native-module-host.js index 5445f3db..f3df63fc 100644 --- a/desktop/native-module-host.js +++ b/desktop/native-module-host.js @@ -119,6 +119,10 @@ const messageHandlers = { return getAutoType().text(str); }, + kbdTextAsKeys(str, mods) { + return kbdTextAsKeys(str, mods); + }, + kbdKeyPress(code, modifiers) { return getAutoType().keyPress(kbdKeyCode(code), kbdModifier(modifiers)); }, @@ -127,10 +131,6 @@ const messageHandlers = { return getAutoType().shortcut(kbdKeyCode(code)); }, - kbdKeyMoveWithCode(down, code, modifiers) { - return getAutoType().keyMoveWithCode(down, kbdKeyCode(code), kbdModifier(modifiers)); - }, - kbdKeyMoveWithModifier(down, modifiers) { return getAutoType().keyMoveWithModifier(down, kbdModifier(modifiers)); }, @@ -209,6 +209,30 @@ function kbdModifier(modifiers) { return modifier; } +function kbdTextAsKeys(str, modifiers) { + const typer = getAutoType(); + const modifier = kbdModifier(modifiers); + let ix = 0; + for (const kc of typer.osKeyCodesForChars(str)) { + const char = str[ix++]; + let effectiveModifier = modifier; + if (kc?.modifier) { + typer.keyMoveWithModifier(true, kc.modifier); + effectiveModifier |= kc.modifier; + } + if (kc) { + typer.keyMoveWithCharacter(true, null, kc.code, effectiveModifier); + typer.keyMoveWithCharacter(false, null, kc.code, effectiveModifier); + } else { + typer.keyMoveWithCharacter(true, char, null, effectiveModifier); + typer.keyMoveWithCharacter(false, char, null, effectiveModifier); + } + if (kc?.modifier) { + typer.keyMoveWithModifier(false, kc.modifier); + } + } +} + function startListener() { process.on('message', ({ callId, cmd, args }) => { Promise.resolve() diff --git a/package-lock.json b/package-lock.json index 58b5f6bd..3253f0bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1982,8 +1982,8 @@ } }, "@keeweb/keeweb-native-modules": { - "version": "https://github.com/keeweb/keeweb-native-modules/releases/download/0.7.7/keeweb-native-modules.tgz", - "integrity": "sha512-rIe5RAFSIW6kHZ2dsf3w5M+7OJxgG6lKMrYxd2nbV+4VJvKK1+Fu14X3jBwoMlSEkqeuyF+GXb78isu6X0hxIQ==" + "version": "https://github.com/keeweb/keeweb-native-modules/releases/download/0.7.9/keeweb-native-modules.tgz", + "integrity": "sha512-xKcm1X89LHQT7JzaYfUW4jIdA0pYREZQ0JDw8ORfIKPhifJF8j7q5xs9Iwu3yEJv7pFNfQIkLRh6pJL80aTkkA==" }, "@sindresorhus/is": { "version": "0.14.0", diff --git a/package.json b/package.json index 9b46a256..b5959b5f 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "@babel/plugin-proposal-optional-chaining": "^7.12.7", "@babel/preset-env": "^7.12.7", "@fortawesome/fontawesome-free": "^5.15.1", - "@keeweb/keeweb-native-modules": "https://github.com/keeweb/keeweb-native-modules/releases/download/0.7.7/keeweb-native-modules.tgz", + "@keeweb/keeweb-native-modules": "https://github.com/keeweb/keeweb-native-modules/releases/download/0.7.9/keeweb-native-modules.tgz", "adm-zip": "^0.5.1", "argon2-browser": "1.15.2", "autoprefixer": "^10.0.4",