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