Files
2025-01-03 20:07:29 -08:00
..
2025-01-03 20:07:29 -08:00
2025-01-03 20:07:29 -08:00
2025-01-03 20:07:29 -08:00

nut.js 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("@mintplex-labs/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("@mintplex-labs/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("@mintplex-labs/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);
    });
});