From 2f1b7f372aa150a48b76103d9d6d003042dacc72 Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Wed, 3 Nov 2021 23:07:13 +0100 Subject: [PATCH 1/2] (#233) Extended key.enum with audio multimedia keys --- lib/key.enum.ts | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/key.enum.ts b/lib/key.enum.ts index 8078b93f..f6f6ef0b 100644 --- a/lib/key.enum.ts +++ b/lib/key.enum.ts @@ -28,6 +28,18 @@ export enum Key { F10, F11, F12, + F13, + F14, + F15, + F16, + F17, + F18, + F19, + F20, + F21, + F22, + F23, + F24, Num0, Num1, @@ -116,17 +128,17 @@ export enum Key { CapsLock, ScrollLock, NumLock, - - F13, - F14, - F15, - F16, - F17, - F18, - F19, - F20, - F21, - F22, - F23, - F24, + + AudioMute, + AudioVolDown, + AudioVolUp, + AudioPlay, + AudioStop, + AudioPause, + AudioPrev, + AudioNext, + AudioRewind, + AudioForward, + AudioRepeat, + AudioRandom } From 653ed2c6a8f72ff370cecaa5db207f4e05ac38eb Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Tue, 7 Dec 2021 23:25:30 +0100 Subject: [PATCH 2/2] (#233) Extended Key mapping --- lib/provider/native/libnut-keyboard.class.ts | 441 ++++++++++--------- 1 file changed, 227 insertions(+), 214 deletions(-) diff --git a/lib/provider/native/libnut-keyboard.class.ts b/lib/provider/native/libnut-keyboard.class.ts index 9f5db146..60846bc1 100644 --- a/lib/provider/native/libnut-keyboard.class.ts +++ b/lib/provider/native/libnut-keyboard.class.ts @@ -1,219 +1,232 @@ import libnut = require("@nut-tree/libnut"); -import { Key } from "../../key.enum"; -import { KeyboardProviderInterface } from "../keyboard-provider.interface"; +import {Key} from "../../key.enum"; +import {KeyboardProviderInterface} from "../keyboard-provider.interface"; export default class KeyboardAction implements KeyboardProviderInterface { - public static KeyLookupMap = new Map([ - [Key.A, "a"], - [Key.B, "b"], - [Key.C, "c"], - [Key.D, "d"], - [Key.E, "e"], - [Key.F, "f"], - [Key.G, "g"], - [Key.H, "h"], - [Key.I, "i"], - [Key.J, "j"], - [Key.K, "k"], - [Key.L, "l"], - [Key.M, "m"], - [Key.N, "n"], - [Key.O, "o"], - [Key.P, "p"], - [Key.Q, "q"], - [Key.R, "r"], - [Key.S, "s"], - [Key.T, "t"], - [Key.U, "u"], - [Key.V, "v"], - [Key.W, "w"], - [Key.X, "x"], - [Key.Y, "y"], - [Key.Z, "z"], - - [Key.F1, "f1"], - [Key.F2, "f2"], - [Key.F3, "f3"], - [Key.F4, "f4"], - [Key.F5, "f5"], - [Key.F6, "f6"], - [Key.F7, "f7"], - [Key.F8, "f8"], - [Key.F9, "f9"], - [Key.F10, "f10"], - [Key.F11, "f11"], - [Key.F12, "f12"], - [Key.F13, "f13"], - [Key.F14, "f14"], - [Key.F15, "f15"], - [Key.F16, "f16"], - [Key.F17, "f17"], - [Key.F18, "f18"], - [Key.F19, "f19"], - [Key.F20, "f20"], - [Key.F21, "f21"], - [Key.F22, "f22"], - [Key.F23, "f23"], - [Key.F24, "f24"], - - [Key.Num0, "0"], - [Key.Num1, "1"], - [Key.Num2, "2"], - [Key.Num3, "3"], - [Key.Num4, "4"], - [Key.Num5, "5"], - [Key.Num6, "6"], - [Key.Num7, "7"], - [Key.Num8, "8"], - [Key.Num9, "9"], - [Key.NumPad0, "numpad_0"], - [Key.NumPad1, "numpad_1"], - [Key.NumPad2, "numpad_2"], - [Key.NumPad3, "numpad_3"], - [Key.NumPad4, "numpad_4"], - [Key.NumPad5, "numpad_5"], - [Key.NumPad6, "numpad_6"], - [Key.NumPad7, "numpad_7"], - [Key.NumPad8, "numpad_8"], - [Key.NumPad9, "numpad_9"], - - [Key.Space, "space"], - [Key.Escape, "escape"], - [Key.Tab, "tab"], - [Key.LeftAlt, "alt"], - [Key.LeftControl, "control"], - [Key.RightAlt, "alt"], - [Key.RightControl, "control"], - - [Key.LeftShift, "shift"], - [Key.LeftSuper, "command"], - [Key.RightShift, "space"], - [Key.RightSuper, "command"], - - [Key.Grave, "~"], - [Key.Minus, "-"], - [Key.Equal, "="], - [Key.Backspace, "backspace"], - [Key.LeftBracket, "["], - [Key.RightBracket, "]"], - [Key.Backslash, "\\"], - [Key.Semicolon, ";"], - [Key.Quote, "'"], - [Key.Return, "enter"], - [Key.Comma, ","], - [Key.Period, "."], - [Key.Slash, "/"], - - [Key.Left, "left"], - [Key.Up, "up"], - [Key.Right, "right"], - [Key.Down, "down"], - - [Key.Print, "printscreen"], - [Key.Pause, null], - [Key.Insert, "insert"], - [Key.Delete, "delete"], - [Key.Home, "home"], - [Key.End, "end"], - [Key.PageUp, "pageup"], - [Key.PageDown, "pagedown"], - - [Key.Add, null], - [Key.Subtract, null], - [Key.Multiply, null], - [Key.Divide, null], - [Key.Decimal, null], - [Key.Enter, "enter"], - - [Key.CapsLock, null], - [Key.ScrollLock, null], - [Key.NumLock, null], - ]); - - public static keyLookup(key: Key): any { - return this.KeyLookupMap.get(key); - } - - private static mapModifierKeys(...keys: Key[]): string[] { - return keys - .map(modifier => KeyboardAction.keyLookup(modifier)) - .filter(modifierKey => modifierKey != null && modifierKey.length > 1); - } - - private static key(key: Key, event: "up" | "down", ...modifiers: Key[]): Promise { - return new Promise((resolve, reject) => { - try { - const nativeKey = KeyboardAction.keyLookup(key); - const modifierKeys = this.mapModifierKeys(...modifiers); - if (nativeKey) { - libnut.keyToggle(nativeKey, event, modifierKeys); - } - resolve(); - } catch (e) { - reject(e); - } - }); - } - - constructor() { - } - - public type(input: string): Promise { - return new Promise((resolve, reject) => { - try { - libnut.typeString(input); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public click(...keys: Key[]): Promise { - return new Promise((resolve, reject) => { - try { - keys.reverse(); - const [key, ...modifiers] = keys; - const nativeKey = KeyboardAction.keyLookup(key); - const modifierKeys = KeyboardAction.mapModifierKeys(...modifiers); - if (nativeKey) { - libnut.keyTap(nativeKey, modifierKeys); - } - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public pressKey(...keys: Key[]): Promise { - return new Promise(async (resolve, reject) => { - try { - keys.reverse(); - const [key, ...modifiers] = keys; - await KeyboardAction.key(key, "down", ...modifiers); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public releaseKey(...keys: Key[]): Promise { - return new Promise(async (resolve, reject) => { - try { - keys.reverse(); - const [key, ...modifiers] = keys; - await KeyboardAction.key(key, "up", ...modifiers); - resolve(); - } catch (e) { - reject(e); - } - }); - } - - public setKeyboardDelay(delay: number): void { - libnut.setKeyboardDelay(delay); - } + public static KeyLookupMap = new Map([ + [Key.A, "a"], + [Key.B, "b"], + [Key.C, "c"], + [Key.D, "d"], + [Key.E, "e"], + [Key.F, "f"], + [Key.G, "g"], + [Key.H, "h"], + [Key.I, "i"], + [Key.J, "j"], + [Key.K, "k"], + [Key.L, "l"], + [Key.M, "m"], + [Key.N, "n"], + [Key.O, "o"], + [Key.P, "p"], + [Key.Q, "q"], + [Key.R, "r"], + [Key.S, "s"], + [Key.T, "t"], + [Key.U, "u"], + [Key.V, "v"], + [Key.W, "w"], + [Key.X, "x"], + [Key.Y, "y"], + [Key.Z, "z"], + + [Key.F1, "f1"], + [Key.F2, "f2"], + [Key.F3, "f3"], + [Key.F4, "f4"], + [Key.F5, "f5"], + [Key.F6, "f6"], + [Key.F7, "f7"], + [Key.F8, "f8"], + [Key.F9, "f9"], + [Key.F10, "f10"], + [Key.F11, "f11"], + [Key.F12, "f12"], + [Key.F13, "f13"], + [Key.F14, "f14"], + [Key.F15, "f15"], + [Key.F16, "f16"], + [Key.F17, "f17"], + [Key.F18, "f18"], + [Key.F19, "f19"], + [Key.F20, "f20"], + [Key.F21, "f21"], + [Key.F22, "f22"], + [Key.F23, "f23"], + [Key.F24, "f24"], + + [Key.Num0, "0"], + [Key.Num1, "1"], + [Key.Num2, "2"], + [Key.Num3, "3"], + [Key.Num4, "4"], + [Key.Num5, "5"], + [Key.Num6, "6"], + [Key.Num7, "7"], + [Key.Num8, "8"], + [Key.Num9, "9"], + [Key.NumPad0, "numpad_0"], + [Key.NumPad1, "numpad_1"], + [Key.NumPad2, "numpad_2"], + [Key.NumPad3, "numpad_3"], + [Key.NumPad4, "numpad_4"], + [Key.NumPad5, "numpad_5"], + [Key.NumPad6, "numpad_6"], + [Key.NumPad7, "numpad_7"], + [Key.NumPad8, "numpad_8"], + [Key.NumPad9, "numpad_9"], + + [Key.Space, "space"], + [Key.Escape, "escape"], + [Key.Tab, "tab"], + [Key.LeftAlt, "alt"], + [Key.LeftControl, "control"], + [Key.RightAlt, "alt"], + [Key.RightControl, "control"], + + [Key.LeftShift, "shift"], + [Key.LeftSuper, "command"], + [Key.RightShift, "space"], + [Key.RightSuper, "command"], + + [Key.Grave, "~"], + [Key.Minus, "-"], + [Key.Equal, "="], + [Key.Backspace, "backspace"], + [Key.LeftBracket, "["], + [Key.RightBracket, "]"], + [Key.Backslash, "\\"], + [Key.Semicolon, ";"], + [Key.Quote, "'"], + [Key.Return, "enter"], + [Key.Comma, ","], + [Key.Period, "."], + [Key.Slash, "/"], + + [Key.Left, "left"], + [Key.Up, "up"], + [Key.Right, "right"], + [Key.Down, "down"], + + [Key.Print, "printscreen"], + [Key.Pause, null], + [Key.Insert, "insert"], + [Key.Delete, "delete"], + [Key.Home, "home"], + [Key.End, "end"], + [Key.PageUp, "pageup"], + [Key.PageDown, "pagedown"], + + [Key.Add, null], + [Key.Subtract, null], + [Key.Multiply, null], + [Key.Divide, null], + [Key.Decimal, null], + [Key.Enter, "enter"], + + [Key.CapsLock, null], + [Key.ScrollLock, null], + [Key.NumLock, null], + + [Key.AudioMute, "audio_mute"], + [Key.AudioVolDown, "audio_vol_down"], + [Key.AudioVolUp, "audio_vol_up"], + [Key.AudioPlay, "audio_play"], + [Key.AudioStop, "audio_stop"], + [Key.AudioPause, "audio_pause"], + [Key.AudioPrev, "audio_prev"], + [Key.AudioNext, "audio_next"], + [Key.AudioRewind, "audio_rewind"], + [Key.AudioForward, "audio_forward"], + [Key.AudioRepeat, "audio_repeat"], + [Key.AudioRandom, "audio_random"] + ]); + + public static keyLookup(key: Key): any { + return this.KeyLookupMap.get(key); + } + + private static mapModifierKeys(...keys: Key[]): string[] { + return keys + .map(modifier => KeyboardAction.keyLookup(modifier)) + .filter(modifierKey => modifierKey != null && modifierKey.length > 1); + } + + private static key(key: Key, event: "up" | "down", ...modifiers: Key[]): Promise { + return new Promise((resolve, reject) => { + try { + const nativeKey = KeyboardAction.keyLookup(key); + const modifierKeys = this.mapModifierKeys(...modifiers); + if (nativeKey) { + libnut.keyToggle(nativeKey, event, modifierKeys); + } + resolve(); + } catch (e) { + reject(e); + } + }); + } + + constructor() { + } + + public type(input: string): Promise { + return new Promise((resolve, reject) => { + try { + libnut.typeString(input); + resolve(); + } catch (e) { + reject(e); + } + }); + } + + public click(...keys: Key[]): Promise { + return new Promise((resolve, reject) => { + try { + keys.reverse(); + const [key, ...modifiers] = keys; + const nativeKey = KeyboardAction.keyLookup(key); + const modifierKeys = KeyboardAction.mapModifierKeys(...modifiers); + if (nativeKey) { + libnut.keyTap(nativeKey, modifierKeys); + } + resolve(); + } catch (e) { + reject(e); + } + }); + } + + public pressKey(...keys: Key[]): Promise { + return new Promise(async (resolve, reject) => { + try { + keys.reverse(); + const [key, ...modifiers] = keys; + await KeyboardAction.key(key, "down", ...modifiers); + resolve(); + } catch (e) { + reject(e); + } + }); + } + + public releaseKey(...keys: Key[]): Promise { + return new Promise(async (resolve, reject) => { + try { + keys.reverse(); + const [key, ...modifiers] = keys; + await KeyboardAction.key(key, "up", ...modifiers); + resolve(); + } catch (e) { + reject(e); + } + }); + } + + public setKeyboardDelay(delay: number): void { + libnut.setKeyboardDelay(delay); + } }