Simulating keyboard input with nut.js
nut.js allows simulation of keyboard input by typing text or pressing / releasing single keys or key combinations.
The nut.js keyboard comes with a config object which allows configuration of its behaviour.
autoDelayMs
keyboard.config.autoDelayMs
configures the delay between keypresses.
type
allows one to type either strings or Keys.
1const {keyboard, Key} = require("@nut-tree/nut-js"); 2 3describe("Keyboard test", () => { 4 it("should open Spotlight on macOS", async () => { 5 await keyboard.type(Key.LeftSuper, Key.Space); 6 await keyboard.type("calculator"); 7 }); 8});
pressKey
will press and hold multiple keys.
1const {keyboard, Key} = require("@nut-tree/nut-js"); 2 3describe("Keyboard test", () => { 4 it("should press and release Alt+F4", async () => { 5 await keyboard.pressKey(Key.LeftAlt, Key.F4); 6 await keyboard.releaseKey(Key.LeftAlt, Key.F4); 7 }); 8});
releaseKey
will release multiple keys again.
1const {keyboard, Key} = require("@nut-tree/nut-js"); 2 3describe("Keyboard test", () => { 4 it("should press and release Alt+F4", async () => { 5 await keyboard.pressKey(Key.LeftAlt, Key.F4); 6 await keyboard.releaseKey(Key.LeftAlt, Key.F4); 7 }); 8});
© 2023