uiohook-napi

repository·master·Indexed 42 days ago

https://github.com/snosme/uiohook-napi

N-API C-bindings for libuiohook that enable Node.js applications to listen to global keyboard, mouse, and wheel events across different operating systems. Version 1.5.5 provides the uIOhook singleton for lifecycle management (start/stop), event listeners for inputs like keydown and mousemove, and methods for synthetic input injection such as keyTap and keyToggle.

Tokens
2.4K
Snippets
7
Records
8
Agent score
4%

What's inside uiohook-napi

  1. Understand Uiohook event data structures

    master

    Depending on the event type, uIOhook emits different data structures. All events include modifier state (altKey, ctrlKey, metaKey, shiftKey) and a timestamp (time).

    UiohookKeyboardEvent

    Used for keydown and keyup.

    • keycode: The numeric code of the key.

    UiohookMouseEvent

    Used for mousedown, mouseup, mousemove, and click.

    • x, y: Coordinates of the mouse.
    • button: The mouse button involved.
    • clicks: Number of clicks.

    UiohookWheelEvent

    Used for wheel.

    • x, y: Coordinates of the wheel event.
    • amount: Scroll amount.
    • direction: WheelDirection (VERTICAL = 3, HORIZONTAL = 4).
    • rotation: Rotation value.
  2. Use uiohook-napi to listen for input events

    master

    The uIOhook object allows you to register event listeners for keyboard, mouse, and wheel inputs. You can listen for specific events like keydown, keyup, mousedown, mouseup, mousemove, click, and wheel. Once listeners are attached, call uIOhook.start() to begin capturing global input events.

    Note that uIOhook provides an on('input', ...) method that acts as a catch-all for any input event type.

    import { uIOhook, UiohookKey } from 'uiohook-napi'
    
    uIOhook.on('keydown', (e) => {
      if (e.keycode === UiohookKey.Q) {
        console.log('Hello!')
      }
    
      if (e.keycode === UiohookKey.Escape) {
        process.exit(0)
      }
    })
    
    uIOhook.start()
  3. UiohookNapi API Reference

    master

    The UiohookNapi interface provides methods for event subscription and synthetic input injection.

    Event Listeners

    • on(event: 'input', listener): Catch-all listener for UiohookKeyboardEvent, UiohookMouseEvent, or UiohookWheelEvent.
    • on(event: 'keydown' | 'keyup', listener): Listens for keyboard events.
    • on(event: 'mousedown' | 'mouseup' | 'mousemove' | 'click', listener): Listens for mouse events.
    • on(event: 'wheel', listener): Listens for mouse wheel events.

    Input Injection

    • keyTap(key: keycode, modifiers?: keycode[]): Simulates a key press and release.
    • keyToggle(key: keycode, toggle: 'down' | 'up'): Simulates pressing a key down or releasing it.
    interface UiohookNapi {
      on(event: 'input', listener: (e: UiohookKeyboardEvent | UiohookMouseEvent | UiohookWheelEvent) => void): this
    
      on(event: 'keydown', listener: (e: UiohookKeyboardEvent) => void): this
      on(event: 'keyup', listener: (e: UiohookKeyboardEvent) => void): this
    
      on(event: 'mousedown', listener: (e: UiohookMouseEvent) => void): this
      on(event: 'mouseup', listener: (e: UiohookMouseEvent) => void): this
      on(event: 'mousemove', listener: (e: UiohookMouseEvent) => void): this
      on(event: 'click', listener: (e: UiohookMouseEvent) => void): this
    
      on(event: 'wheel', listener: (e: UiohookWheelEvent) => void): this
    
      keyTap(key: keycode, modifiers?: keycode[])
      keyToggle(key: keycode, toggle: 'down' | 'up')
    }
  4. Uiohook Event Data Structures

    master

    When an event is triggered, the listener receives an event object containing the following properties:

    UiohookKeyboardEvent

    • altKey: boolean
    • ctrlKey: boolean
    • metaKey: boolean
    • shiftKey: boolean
    • keycode: number

    UiohookMouseEvent

    • altKey: boolean
    • ctrlKey: boolean
    • metaKey: boolean
    • shiftKey: boolean
    • x: number
    • y: number
    • button: unknown
    • clicks: number

    UiohookWheelEvent

    • altKey: boolean
    • ctrlKey: boolean
    • metaKey: boolean
    • shiftKey: boolean
    • x: number
    • y: number
    • clicks: number
    • amount: number
    • direction: WheelDirection
    • rotation: number
    export interface UiohookKeyboardEvent {
      altKey: boolean
      ctrlKey: boolean
      metaKey: boolean
      shiftKey: boolean
      keycode: number
    }
    
    export interface UiohookMouseEvent {
      altKey: boolean
      ctrlKey: boolean
      metaKey: boolean
      shiftKey: boolean
      x: number
      y: number
      button: unknown
      clicks: number
    }
    
    export interface UiohookWheelEvent {
      altKey: boolean
      ctrlKey: boolean
      metaKey: boolean
      shiftKey: boolean
      x: number
      y: number
      clicks: number
      amount: number
      direction: WheelDirection
      rotation: number
    }
  5. Listen to input events with uIOhook

    master

    uIOhook is an EventEmitter that emits various input events. You can listen to a generic input event for all activity or specific events for keyboard, mouse, and wheel actions.

    Available Events

    • input: Emits any UiohookKeyboardEvent, UiohookMouseEvent, or UiohookWheelEvent.
    • keydown / keyup: Emits UiohookKeyboardEvent.
    • mousedown / mouseup / mousemove / click: Emits UiohookMouseEvent.
    • wheel: Emits UiohookWheelEvent.
    import { uIOhook, EventType } from 'uiohook-napi';
    
    uIOhook.on('keydown', (e) => {
      console.log(`Key pressed: ${e.keycode}`);
    });
    
    uIOhook.on('click', (e) => {
      console.log(`Mouse clicked at: ${e.x}, ${e.y}`);
    });
    
    uIOhook.on('wheel', (e) => {
      console.log(`Wheel direction: ${e.direction}`);
    });
  6. Control uIOhook lifecycle and simulate input

    master

    The uIOhook singleton provides methods to start/stop the global input hook and simulate keyboard input.

    Lifecycle Management

    • start(cb): Starts the global input hook. It accepts a callback that is triggered on events.
    • stop(): Stops the global input hook.

    Simulating Keyboard Input

    • keyTap(key: number, modifiers: number[] = []): Simulates a key press. If modifiers are provided, it simulates holding down the modifier keys, tapping the target key, and then releasing the modifiers.
    • keyToggle(key: number, toggle: 'down' | 'up'): Simulates either pressing down ('down') or releasing ('up') a specific key.
    import { uIOhook, UiohookKey } from 'uiohook-napi';
    
    // Start listening
    uIOhook.start();
    
    // Simulate pressing 'Enter'
    uIOhook.keyTap(UiohookKey.Enter);
    
    // Simulate Ctrl + A
    uIOhook.keyTap(UiohookKey.A, [UiohookKey.Ctrl]);
    
    // Stop listening
    uIOhook.stop();
  7. Reference EventType constants

    master

    The EventType enum defines the type of input event being processed. Use these to identify the nature of an event in a generic input listener.

    ConstantValueDescription
    EVENT_KEY_PRESSED4Keyboard key down
    EVENT_KEY_RELEASED5Keyboard key up
    EVENT_MOUSE_CLICKED6Mouse button click
    EVENT_MOUSE_PRESSED7Mouse button down
    EVENT_MOUSE_RELEASED8Mouse button up
    EVENT_MOUSE_MOVED9Mouse movement
    EVENT_MOUSE_WHEEL11Mouse wheel scroll
    export enum EventType {
      EVENT_KEY_PRESSED = 4,
      EVENT_KEY_RELEASED = 5,
      EVENT_MOUSE_CLICKED = 6,
      EVENT_MOUSE_PRESSED = 7,
      EVENT_MOUSE_RELEASED = 8,
      EVENT_MOUSE_MOVED = 9,
      EVENT_MOUSE_WHEEL = 11
    }
  8. Reference UiohookKey constants

    master

    The UiohookKey object contains the numeric keycodes used for both identifying keys in events and simulating key presses via keyTap or keyToggle.

    Commonly used keys:

    • Escape: 0x0001
    • Backspace: 0x000E
    • Tab: 0x000F
    • Enter: 0x001C
    • Space: 0x0039
    • ArrowLeft: 0xE04B
    • ArrowUp: 0xE048
    • ArrowRight: 0xE04D
    • ArrowDown: 0xE050
    • Ctrl: 0x001D (Left)
    • Alt: 0x0038 (Left)
    • Shift: 0x002A (Left)
    • Meta: 0x0E5B (Left)

    Note: For Numpad keys, codes often include a high bit (e.g., 0xEE00 | code).

    export const UiohookKey = {
      Backspace: 0x000E,
      Tab: 0x000F,
      Enter: 0x001C,
      // ... (full list available in source)
    } as const;