Input plugins

@nut-tree/bolt


Installation

npm i @nut-tree/bolt

Description

@nut-tree/bolt is an alternative low-level provider for nut.js. It can replace the default @nut-tree/nut-js low-level provider, @nut-tree/libnut, and comes with additional features.

The most significant features at the time of writing are:

  • Proper unicode support
  • An implementation of the WindowFinderInterface to find windows by title
  • Advanced window functions like minimize, restore, resize and move
  • Input monitoring for mouse and keyboard events on Windows and macOS
  • Full access to all core packages

Usage

@nut-tree/bolt exports a set of functions for fine-grained control over each low-level provider.

  • useBoltKeyboard(): Replaces the default keyboard provider with @nut-tree/bolt's keyboard implementation
  • useBoltMouse(): Replaces the default mouse provider with @nut-tree/bolt's mouse implementation
  • useBoltScreen(): Replaces the default screen provider with @nut-tree/bolt's screen implementation
  • useBoltWindows(): Replaces the default window provider with @nut-tree/bolt's window implementation
  • useBoltWindowFinder(): Registers the @nut-tree/bolt window finder implementation
  • useBoltInputMonitor(): Registers the @nut-tree/bolt input monitor implementation
const {keyboard, imageResource} = require("@nut-tree/nut-js");
const {useBoltKeyboard} = require("@nut-tree/bolt");

useBoltKeyboard(); // From this point on all keyboard interactions will be handled by @nut-tree/bolt

(async () => {
    await keyboard.type("@nut-tree/bolt is awesome!");
})();

In case you want to use all available @nut-tree/bolt providers, you can use the useBolt() function:

const {screen, imageResource} = require("@nut-tree/nut-js");
const {useBolt} = require("@nut-tree/bolt");

useBolt(); // Now we have all @nut-tree/bolt providers set up, including the window finder

(async () => {
    const wnd = await screen.find(windowWithTitle(/some.*regex/));
})();

Input Monitoring (since v2.2.0)

@nut-tree/bolt implements the InputMonitor interface for mouse and keyboard events on Windows and macOS. This allows you to monitor and record mouse and keyboard input events and react to them in real-time.

It's important to distinguish Input Monitoring from Global Hotkeys:

  • Non-Intrusive: Input Monitoring observes events without overriding existing functionalities.
  • Flexible Control: Unlike global hotkeys, which can interfere with system shortcuts, Input Monitoring allows for passive observation and reaction.

Both the mouse and keyboard instances in nut.js are now EventEmitters. On can access captured events by registering callbacks to respond to specific keyboard commands or mouse positions.

import {mouse, keyboard, system, Key} from "@nut-tree/nut-js";
import {useBoltInputMonitor} from "@nut-tree/bolt";

useBoltInputMonitor();

// Register a callback for keyDown events
keyboard.on("keyDown", async (evt) => {
    if (isKeyEvent(evt, Key.Escape) && withModifiers(evt, [Key.LeftControl])) {
        // User pressed Ctrl + Esc, exit the script immediately
        system.stopMonitoringInputEvents();
    }
});

mouse.on("mouseDown", async (evt) => {
    console.log(`Mouse button ${evt.button} clicked at ${evt.targetPoint.x}, ${evt.targetPoint.y}`);
});

// Start monitoring input events
// Without this call, the registered event listeners will not be triggered
// ATTENTION: Your script will not terminate until you stop monitoring input events
system.startMonitoringInputEvents();

Buy