36 lines
1.0 KiB
JavaScript
36 lines
1.0 KiB
JavaScript
|
const {keyboard, Key } = require('@nut-tree/nut-js');
|
||
|
|
||
|
/**
|
||
|
* A few predefined shortcuts for common actions like copy, paste, etc...
|
||
|
*/
|
||
|
const SHORTCUTS = {
|
||
|
copy: [Key.LeftControl, Key.C],
|
||
|
paste: [Key.LeftControl, Key.V],
|
||
|
cut: [Key.LeftControl, Key.X],
|
||
|
altf4: [Key.LeftAlt, Key.F4]
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Will type a string with a provided delay.
|
||
|
* @param {A string or sequence of keys to type. Wrapper for @nut-tree/nut-js keyboard class} string
|
||
|
* @param {Delay between keypresses in milliseconds.} delay
|
||
|
*/
|
||
|
function typeString(string, delay = 0){
|
||
|
keyboard.config.autoDelayMs = delay
|
||
|
keyboard.type(string);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Will type a sequence of keys.
|
||
|
* You can also call on some predefined shortcuts.
|
||
|
* @param {An array of keys using the nutjs key import or key codes} keys
|
||
|
* @example
|
||
|
* typeCommnad(Key.LeftControl, Key.C)
|
||
|
* typeCommand(SHORTCUTS.copy)
|
||
|
*/
|
||
|
function typeCommand(keys){
|
||
|
keyboard.pressKey(...keys);
|
||
|
keyboard.releaseKey(...keys)
|
||
|
}
|
||
|
|
||
|
module.exports = {SHORTCUTS, typeCommand, typeString}
|