typing key codes

This commit is contained in:
antelle 2021-01-30 18:24:42 +01:00
parent a750e80ea1
commit e13002bab3
No known key found for this signature in database
GPG Key ID: 63C9777AAB7C563C
5 changed files with 61 additions and 29 deletions

View File

@ -64,9 +64,8 @@ const ModMap = {
};
class AutoTypeEmitter {
constructor(callback, windowId) {
constructor(callback) {
this.callback = callback;
this.windowId = windowId;
this.mod = {};
}
@ -80,18 +79,23 @@ class AutoTypeEmitter {
}
text(str) {
this.withCallback(NativeModules.kbdText(str)); // TODO: how to handle modifiers?
this.withCallback(NativeModules.kbdText(str));
}
key(key) {
const mods = Object.keys(this.mod);
if (typeof key === 'number') {
// TODO: platform-specific keycodes
this.withCallback(
NativeModules.kbdKeyMoveWithCharacter(true, 0, key, mods).then(() =>
NativeModules.kbdKeyMoveWithCharacter(false, 0, key, mods)
)
);
} else {
if (!KeyMap[key]) {
return this.callback('Bad key: ' + key);
}
const code = KeyMap[key];
this.withCallback(NativeModules.kbdKeyPress(code)); // TODO: modifier
this.withCallback(NativeModules.kbdKeyPress(code, mods));
}
}

View File

@ -236,6 +236,18 @@ if (Launcher) {
kbdShortcut(code, modifiers) {
return this.call('kbd-shortcut', code, modifiers);
},
kbdKeyMoveWithCode(down, code, modifiers) {
return this.call('kbd-key-move-with-code', down, code, modifiers);
},
kbdKeyMoveWithModifier(down, modifiers) {
return this.call('kbd-key-move-with-modifier', down, modifiers);
},
kbdKeyMoveWithCharacter(down, character, code, modifiers) {
return this.call('kbd-key-move-with-character', down, character, code, modifiers);
}
};

View File

@ -120,30 +120,23 @@ const messageHandlers = {
},
'kbd-key-press'(code, modifiers) {
const { KeyCode, Modifier } = reqNative('keyboard-auto-type');
code = KeyCode[code];
if (!code) {
throw new Error(`Bad code: ${code}`);
}
let modifier = Modifier.None;
if (modifiers) {
for (const mod of modifiers) {
if (!Modifier[mod]) {
throw new Error(`Bad modifier: ${mod}`);
}
modifier |= Modifier[mod];
}
}
return getAutoType().keyPress(code, modifier);
return getAutoType().keyPress(kbdKeyCode(code), kbdModifier(modifiers));
},
'kbd-shortcut'(code) {
const { KeyCode } = reqNative('keyboard-auto-type');
code = KeyCode[code];
if (!code) {
throw new Error(`Bad code: ${code}`);
}
return getAutoType().shortcut(code);
return getAutoType().shortcut(kbdKeyCode(code));
},
'kbd-key-move-with-code'(down, code, modifiers) {
return getAutoType().keyMoveWithCode(down, kbdKeyCode(code), kbdModifier(modifiers));
},
'kbd-key-move-with-modifier'(down, modifiers) {
return getAutoType().keyMoveWithModifier(down, kbdModifier(modifiers));
},
'kbd-key-move-with-character'(down, character, code, modifiers) {
return getAutoType().keyMoveWithCharacter(down, character, code, kbdModifier(modifiers));
}
};
@ -186,6 +179,29 @@ function getAutoType() {
return autoType;
}
function kbdKeyCode(code) {
const { KeyCode } = reqNative('keyboard-auto-type');
const kbdCode = KeyCode[code];
if (!kbdCode) {
throw new Error(`Bad code: ${code}`);
}
return kbdCode;
}
function kbdModifier(modifiers) {
const { Modifier } = reqNative('keyboard-auto-type');
let modifier = Modifier.None;
if (modifiers) {
for (const mod of modifiers) {
if (!Modifier[mod]) {
throw new Error(`Bad modifier: ${mod}`);
}
modifier |= Modifier[mod];
}
}
return modifier;
}
function startListener() {
process.on('message', ({ callId, cmd, args }) => {
Promise.resolve()

4
package-lock.json generated
View File

@ -1982,8 +1982,8 @@
}
},
"@keeweb/keeweb-native-modules": {
"version": "https://github.com/keeweb/keeweb-native-modules/releases/download/0.7.3/keeweb-native-modules.tgz",
"integrity": "sha512-68NbKwqIOtrHDx/pr7jYdP50kiByqywu4L0Ow1/vR0HplFwoaOmCknHeZFcK5C84qk9Z+XyNwrmrHkVjE7V3Wg=="
"version": "https://github.com/keeweb/keeweb-native-modules/releases/download/0.7.4/keeweb-native-modules.tgz",
"integrity": "sha512-IGTr/DmZ5wDN0PPSfJ48EEz5srknq2n2Fl4WF9qcCLLEZXa3S/Q56lSV9eRzIvJqwRwM6OCKasPTvJyFAuV34g=="
},
"@sindresorhus/is": {
"version": "0.14.0",

View File

@ -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.3/keeweb-native-modules.tgz",
"@keeweb/keeweb-native-modules": "https://github.com/keeweb/keeweb-native-modules/releases/download/0.7.4/keeweb-native-modules.tgz",
"adm-zip": "^0.5.1",
"argon2-browser": "1.15.2",
"autoprefixer": "^10.0.4",