Huakun

monio-napi

Cross-platform input monitoring for Node.js, powered by monio and napi-rs.

GitHub: https://github.com/HuakunShen/monio-napi

Features

  • Cross-platform: macOS, Windows, and Linux (X11) support
  • Proper drag detection: Distinguishes MouseDragged from MouseMoved events
  • Event listening: Non-blocking background listener with callback
  • Event simulation: Programmatically generate keyboard and mouse events
  • Display queries: Get monitor info, scale factor, refresh rate, system settings
  • Native performance: Rust native addon via N-API, no electron/node-gyp required

Installation

npm install monio-napi
# or
pnpm add monio-napi

Usage

Listening for Events

import { startListen, EventTypeJs } from "monio-napi";

const hook = startListen((event) => {
  switch (event.eventType) {
    case EventTypeJs.KeyPressed:
      console.log("Key pressed:", event.keyboard?.key);
      break;
    case EventTypeJs.MouseDragged:
      console.log(`Dragging at (${event.mouse?.x}, ${event.mouse?.y})`);
      break;
    case EventTypeJs.MouseWheel:
      console.log("Scroll:", event.wheel?.direction);
      break;
  }
});

// Stop when done
hook.stop();

Simulating Input

import {
  simulateMouseMove,
  simulateMouseClick,
  simulateKeyTap,
  ButtonJs,
  KeyJs,
} from "monio-napi";

// Move mouse to position
simulateMouseMove(100, 200);

// Click
simulateMouseClick(ButtonJs.Left);

// Type a key
simulateKeyTap(KeyJs.KeyA);

Display Information

import { getDisplays, getPrimaryDisplay, getSystemSettings } from "monio-napi";

const displays = getDisplays();
for (const display of displays) {
  console.log(
    `Display ${display.id}: ${display.bounds.width}x${display.bounds.height}`,
  );
  console.log(
    `  Scale: ${display.scaleFactor}, Refresh: ${display.refreshRate}Hz`,
  );
}

Platform Notes

  • macOS: Requires Accessibility permissions
  • Windows: No special permissions required for hooking
  • Linux: Uses X11 (XRecord for capture, XTest for simulation)

On this page