Skip to main content

Keyboard Control

nut.js allows to simulate keyboard input by typing text or pressing / releasing single keys or key combinations.

Configuration#

The nut.js keyboard comes with a config object which allows to configure it's behaviour.

autoDelayMs#

keyboard.config.autoDelayMs configures the delay between keypresses.

type#

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#

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#

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);
});
});