@cartridge-engine/keyboard-engine
    Preparing search index...

    Class Mouse

    Represents and manages mouse events in a TTY environment.

    This class is a facade that composes smaller, focused components:

    • TTYController: Manages terminal state and stream I/O
    • MouseEventManager: Handles event emission and detection
    • EventStreamFactory: Creates async generator streams
    • MouseConvenienceMethods: Provides promise-based convenience methods

    Automatic Cleanup: Mouse instances automatically register for cleanup when enable() is called. If a Mouse instance is garbage collected without explicit cleanup via disable() or destroy(), the TTYController ensures that stdin event listeners are removed to prevent memory leaks.

    Recommended Cleanup: Despite automatic cleanup, it's still recommended to explicitly call destroy() when done with a Mouse instance for immediate and predictable resource release.

    Index
    • Constructs a new Mouse instance.

      Parameters

      • Optionaloptions: MouseOptions

        Optional configuration options for mouse behavior and dependencies.

        Configuration options for the Mouse class. All properties are optional and provide sensible defaults.

        • OptionalclickDistanceThreshold?: number

          Maximum allowed distance (in cells) between press and release to qualify as a click. Defaults to 1, meaning the press and release must be within 1 cell in both X and Y directions. Set to 0 to require exact same position, or higher values to allow more movement.

        • OptionaldegradedDedupDistance?: number

          In degraded mode, presses within this many cells of the last synthesized click are treated as the same click (one terminal click can be reported as several button presses at the same spot). Defaults to 1.

        • OptionaldegradedDedupWindowMs?: number

          In degraded mode, how long (ms) a synthesized click's position is deduplicated for. A press at the same spot after this window is a NEW click, not a duplicate report. Defaults to 300. Set to 0 to dedupe only same-millisecond bursts (practically disables position dedup).

        • Optionalemitter?: EventEmitter

          The event emitter to use for emitting mouse events. Defaults to a new EventEmitter instance.

        • OptionalinputStream?: ReadableStreamWithEncoding

          The readable stream to listen for mouse events on. Defaults to process.stdin.

        • OptionaloutputStream?: NodeJS.WriteStream

          The writable stream to send control sequences to. Defaults to process.stdout.

        • OptionalpressStormThreshold?: number

          Consecutive press events with no release in between that trigger degraded "press-is-click" mode. Some terminals (e.g. VS Code's built-in terminal) stop reporting releases after multiple buttons are pressed simultaneously; without this fallback clicks would never be synthesized there. Defaults to 3. Set to Infinity to disable degraded mode.

        • OptionalpressStormWindowMs?: number

          How long (ms) presses are allowed to span while still counting toward pressStormThreshold. A press arriving after this window restarts the count. Defaults to 500. This prevents a slow multi-button press on a well-behaved terminal from spuriously entering degraded mode. Set to Infinity to count presses without any time limit.

        • OptionalsetRawMode?: (mode: boolean) => void

          Custom function to set raw mode on the input stream. If not provided, defaults to inputStream.setRawMode.

          This is useful for testing or for custom terminal behavior.

      Returns Mouse

      // Default configuration
      const mouse = new Mouse();

      // Custom streams
      const mouse2 = new Mouse({
      inputStream: customStdin,
      outputStream: customStdout,
      });

      // Custom emitter
      const mouse3 = new Mouse({
      emitter: myEventEmitter,
      });

      // Testing with mock setRawMode
      const mockSetRawMode = vi.fn();
      const mouse4 = new Mouse({ setRawMode: mockSetRawMode });

      // Custom click threshold
      const mouse5 = new Mouse({ clickDistanceThreshold: 0 });

      // All options combined
      const mouse6 = new Mouse({
      emitter: myEventEmitter,
      inputStream: customStdin,
      outputStream: customStdout,
      setRawMode: mockSetRawMode,
      clickDistanceThreshold: 5,
      });
    SupportCheckResult: {
        NotTTY: "not_tty";
        OutputNotTTY: "output_not_tty";
        Supported: "supported";
    } = ...

    Result type for terminal capability checks.

    Type Declaration

    • ReadonlyNotTTY: "not_tty"

      Input stream is not a TTY

    • ReadonlyOutputNotTTY: "output_not_tty"

      Output stream is not a TTY

    • ReadonlySupported: "supported"

      Mouse events are supported

    • Returns an async generator that yields move events at most once per specified interval.

      This method provides debounced move events, reducing event frequency for smooth animations and performance optimization. Unlike eventsOf('move') which yields every move event, this method waits for a quiet period before emitting, ensuring you only get events at a controlled rate.

      Debouncing Behavior:

      • Move events are collected during the debounce interval
      • Only the most recent event is yielded after the interval elapses
      • If the mouse continues moving, the timer restarts with each new event
      • This is ideal for UI updates, animations, and position tracking where you want to avoid excessive redraws

      Cancellation with AbortSignal: The async generator supports cancellation through the signal option. When the provided AbortSignal is aborted, the generator will stop immediately and clean up all listeners.

      Cleanup: The generator automatically cleans up event listeners and timers when:

      • The iteration loop completes (breaks or returns)
      • An error is thrown
      • The abort signal is triggered

      Parameters

      • Optionaloptions: { interval?: number; signal?: AbortSignal }

        Configuration for the debounced event stream.

        • Optionalinterval?: number

          Minimum time in milliseconds between yielded events. Defaults to 16 (~60fps).

        • Optionalsignal?: AbortSignal

          An AbortSignal to cancel the async generator and clean up resources.

      Returns AsyncGenerator<XtermMouseEvent>

      A mouse move event object containing x, y, button, and action properties.

      When the abort signal is triggered or a mouse event stream error occurs.

      const mouse = new Mouse();
      mouse.enable();

      // Track mouse position at ~60fps for smooth cursor following
      for await (const event of mouse.debouncedMoveEvents()) {
      console.log(`Mouse at ${event.x}, ${event.y}`);
      }
      // Slower update rate (30fps) for less frequent UI updates
      for await (const event of mouse.debouncedMoveEvents({ interval: 33 })) {
      updateCursorPosition(event.x, event.y);
      }
      // Debounced move events with cancellation
      const controller = new AbortController();
      setTimeout(() => controller.abort(), 10000); // Stop after 10 seconds

      try {
      for await (const event of mouse.debouncedMoveEvents({ signal: controller.signal })) {
      // Smooth animation update at 60fps
      renderFrame(event.x, event.y);
      }
      } catch (err) {
      if (err instanceof MouseError && err.message.includes('aborted')) {
      console.log('Animation stopped');
      }
      }
      // Comparing debounced vs raw move events
      const mouse = new Mouse();
      mouse.enable();

      // Raw: Can fire hundreds of times per second
      for await (const event of mouse.eventsOf('move')) {
      console.log('Raw move'); // May print too fast to read
      if (event.x > 50) break;
      }

      // Debounced: Controlled rate, easier to process
      for await (const event of mouse.debouncedMoveEvents({ interval: 100 })) {
      console.log('Debounced move'); // Prints at most 10 times per second
      if (event.x > 50) break;
      }
    • Disables mouse tracking and removes all event listeners.

      Recommended for Immediate Cleanup: This method is the recommended way to clean up a Mouse instance when you're done with it. While automatic cleanup via FinalizationRegistry prevents memory leaks on garbage collection, calling destroy() explicitly ensures immediate and predictable resource release with no dependency on GC timing.

      Idempotent: Calling this method multiple times is safe and has no additional effect.

      Side Effects:

      • Calls disable() to stop mouse tracking and restore stream state
      • Unregisters from the FinalizationRegistry to prevent duplicate cleanup
      • Removes all event listeners from the internal event emitter
      • Sets the Mouse instance to a non-functional state

      Returns void

      • disable to disable mouse tracking without removing event listeners
      • enable to enable mouse tracking
      const mouse = new Mouse();
      mouse.enable();

      // ... use mouse instance ...

      // Always destroy when done for immediate cleanup
      mouse.destroy();
    • Enables mouse event tracking.

      This method activates mouse event capture by putting the input stream into raw mode and sending the appropriate ANSI escape sequences to enable mouse tracking in the terminal.

      TTY Requirement: This method requires the input stream to be a TTY (terminal). Mouse events cannot be captured when the input is piped, redirected, or running in a non-interactive environment. Check process.stdin.isTTY before calling this method.

      Error Handling: This method throws a MouseError if:

      • The input stream is not a TTY (interactive terminal)
      • The stream cannot be put into raw mode
      • The terminal does not support the mouse tracking ANSI codes

      Automatic Cleanup: When enable() is called, the Mouse instance registers with a FinalizationRegistry. If the instance is garbage collected without explicit cleanup via disable() or destroy(), the registry will automatically remove the stdin listener and restore stream state to prevent memory leaks. This is a safety net - explicit cleanup via destroy() is still recommended for immediate and predictable resource release.

      Side Effects:

      • The input stream is switched to raw mode (character-by-character input)
      • The input encoding is set to UTF-8
      • The input stream is resumed if paused
      • ANSI escape codes are written to the output stream to enable mouse tracking
      • The original stream settings are preserved for restoration on disable()
      • The Mouse instance is registered with the FinalizationRegistry for automatic cleanup

      Returns void

      If the input stream is not a TTY

      If enabling mouse tracking fails

      • disable to disable tracking and restore the stream
      • destroy for recommended cleanup method
      const mouse = new Mouse();

      if (process.stdin.isTTY) {
      mouse.enable();
      mouse.on('press', (event) => {
      console.log(`Pressed at ${event.x}, ${event.y}`);
      });
      } else {
      console.error('Mouse tracking requires a TTY');
      }
    • Returns an async generator that yields mouse events of a specific type.

      This method provides a convenient way to iterate over mouse events using async/await syntax. The async generator will yield events as they occur, allowing for clean and readable event handling code.

      Cancellation with AbortSignal: The async generator supports cancellation through the signal option. When the provided AbortSignal is aborted, the generator will throw a MouseError and clean up all listeners. This is particularly useful for implementing timeout functionality or user-initiated cancellation.

      Queue Management:

      • By default, events are queued up to maxQueue (default: 100, max: 1000)
      • When latestOnly is true, only the most recent event is buffered, dropping intermediate events
      • This is useful for high-frequency events like 'move' where you only care about the latest position

      Error Handling: Errors from the mouse event stream will be thrown from the generator, allowing for try/catch error handling in the iteration loop.

      Cleanup: The generator automatically cleans up event listeners when:

      • The iteration loop completes (breaks or returns)
      • An error is thrown
      • The abort signal is triggered

      Parameters

      • type: MouseEventAction

        The type of mouse event to listen for (e.g., 'press', 'drag', 'wheel').

      • Optionaloptions: { latestOnly?: boolean; maxQueue?: number; signal?: AbortSignal }

        Configuration for the event stream.

        • OptionallatestOnly?: boolean

          If true, only the latest event is buffered. Defaults to false.

        • OptionalmaxQueue?: number

          The maximum number of events to queue. Defaults to 100, with a maximum of 1000.

        • Optionalsignal?: AbortSignal

          An AbortSignal to cancel the async generator and clean up resources.

      Returns AsyncGenerator<XtermMouseEvent>

      A mouse event object containing x, y, button, and action properties.

      When the abort signal is triggered or a mouse event stream error occurs.

      const mouse = new Mouse();
      mouse.enable();

      // Collect 5 mouse clicks
      const clicks: MouseEvent[] = [];
      for await (const event of mouse.eventsOf('click')) {
      clicks.push(event);
      console.log(`Click at ${event.x}, ${event.y}`);
      if (clicks.length >= 5) break;
      }
      mouse.disable();
      // Track mouse movement with cancellation after 5 seconds
      const controller = new AbortController();
      setTimeout(() => controller.abort(), 5000);

      try {
      for await (const event of mouse.eventsOf('move', { signal: controller.signal })) {
      console.log(`Mouse moved to ${event.x}, ${event.y}`);
      }
      } catch (err) {
      if (err instanceof MouseError && err.message.includes('aborted')) {
      console.log('Tracking stopped after timeout');
      } else {
      throw err;
      }
      }
      // Track only the latest mouse position (for high-frequency events)
      const mouse = new Mouse();
      mouse.enable();

      // Display cursor position updates
      for await (const event of mouse.eventsOf('move', { latestOnly: true })) {
      // Clear line and show position
      process.stdout.write(`\r\x1b[KPosition: ${event.x}, ${event.y}`);
      }
      // Implement drag detection with user cancellation
      const controller = new AbortController();

      // Listen for Ctrl+C to cancel
      process.stdin.setRawMode(true);
      process.stdin.on('data', (key) => {
      if (key[0] === 3) { // Ctrl+C
      controller.abort();
      }
      });

      try {
      for await (const event of mouse.eventsOf('drag', { signal: controller.signal })) {
      console.log(`Dragging at ${event.x}, ${event.y} with button ${event.button}`);
      }
      } catch (err) {
      if (err instanceof MouseError && err.message.includes('aborted')) {
      console.log('\nDrag tracking cancelled by user');
      }
      } finally {
      mouse.disable();
      }
    • Gets the last known mouse position synchronously.

      This method immediately returns the last cached mouse position from move or drag events, without waiting for new events. Returns null if no mouse movement has occurred yet.

      No Waiting: Unlike getMousePosition(), this method never waits - it returns the cached position immediately or null if unavailable.

      Use Cases:

      • When you need immediate position access without awaiting
      • To check if mouse has moved yet (null check)
      • For non-async contexts where you can't use await

      Returns MousePosition | null

      The last known position as { x, y }, or null if no movement yet.

      const mouse = new Mouse();
      mouse.enable();

      // Returns null if mouse hasn't moved yet
      const pos = mouse.getLastPosition();
      if (pos) {
      console.log(`Mouse at ${pos.x}, ${pos.y}`);
      } else {
      console.log('No movement yet');
      }
      // Use in synchronous context
      mouse.on('move', () => {
      const pos = mouse.getLastPosition();
      console.log(`Current: ${pos?.x}, ${pos?.y}`);
      });
      // Combine with async version for fallback
      let pos = mouse.getLastPosition();
      if (!pos) {
      pos = await mouse.getMousePosition();
      }
    • Gets the current mouse position, returning immediately if available.

      This method returns the last known mouse position from move or drag events. If the mouse has moved since tracking was enabled, the position is returned immediately without waiting. Otherwise, it waits for the next move event.

      Cached Position: The method maintains an internal cache of the last position from move or drag events. This allows for instant position retrieval without waiting for new events.

      Timeout: The method will reject with a MouseError if the timeout is exceeded while waiting for the first move event.

      Cancellation: The method can be cancelled early using an AbortSignal.

      Parameters

      • Optionaloptions: { signal?: AbortSignal; timeout?: number }

        Configuration options for the wait operation.

        • Optionalsignal?: AbortSignal

          An AbortSignal to cancel the operation early.

        • Optionaltimeout?: number

          Maximum time to wait in milliseconds. Defaults to 30000 (30 seconds).

      Returns Promise<MousePosition>

      A promise that resolves with the x, y coordinates.

      If timeout is exceeded or operation is aborted.

      const mouse = new Mouse();
      mouse.enable();

      try {
      // If mouse has moved, returns immediately
      // Otherwise waits for first move event
      const { x, y } = await mouse.getMousePosition();
      console.log(`Mouse is at ${x}, ${y}`);
      } finally {
      mouse.disable();
      }
      // Get position without waiting (after mouse has moved)
      mouse.on('move', () => {
      // This will resolve immediately since we have a cached position
      mouse.getMousePosition().then(({ x, y }) => {
      console.log(`Current position: ${x}, ${y}`);
      });
      });
      // Use with custom timeout
      const { x, y } = await mouse.getMousePosition({ timeout: 5000 });
    • Checks if mouse event emission is currently paused.

      This method returns the current pause state of mouse event emission. When paused, no mouse events will be emitted, but terminal mouse mode remains active.

      Independent from enabled state: The paused state is independent from the enabled state. You can have:

      • enabled=true, paused=false: Normal operation, events are emitted
      • enabled=true, paused=true: Terminal mouse mode active, but no events emitted
      • enabled=false, paused=false: Terminal mouse mode inactive, no events emitted
      • enabled=false, paused=true: Terminal mouse mode inactive, pause state preserved

      Difference from isEnabled():

      • isPaused(): Checks if event emission is paused (state flag only)
      • isEnabled(): Checks if terminal mouse mode is active (includes terminal state)

      Returns boolean

      True if event emission is paused, false otherwise.

      • pause to pause event emission
      • resume to resume event emission
      • isEnabled to check if terminal mouse mode is enabled
      const mouse = new Mouse();
      mouse.enable();

      console.log(mouse.isPaused()); // false

      mouse.pause();
      console.log(mouse.isPaused()); // true

      mouse.resume();
      console.log(mouse.isPaused()); // false
      // Comparing isPaused() vs isEnabled()
      const mouse = new Mouse();

      mouse.enable();
      console.log(mouse.isEnabled()); // true (terminal mouse mode active)
      console.log(mouse.isPaused()); // false (events are being emitted)

      mouse.pause();
      console.log(mouse.isEnabled()); // true (terminal mouse mode still active!)
      console.log(mouse.isPaused()); // true (events are paused)

      mouse.disable();
      console.log(mouse.isEnabled()); // false (terminal mouse mode inactive)
      console.log(mouse.isPaused()); // true (pause state is preserved)
      // Practical use: Check state before performing operations
      const mouse = new Mouse();
      mouse.enable();

      function performSensitiveOperation() {
      // Save current state
      const wasPaused = mouse.isPaused();

      // Ensure we're paused during the operation
      mouse.pause();

      // ... perform operation ...

      // Restore previous state
      if (!wasPaused) {
      mouse.resume();
      }
      }
    • Removes a listener for a specific mouse event.

      Type Inference: This method uses the same type inference as on() to ensure type safety when removing listeners.

      Type Parameters

      Parameters

      • event: T

        The name of the event to stop listening for.

      • listener: T extends "error" ? (error: Error) => void : ListenerFor<T>

        The callback function to remove.

      Returns EventEmitter

      The event emitter instance.

      on to add a listener

      const mouse = new Mouse();
      const handler = (event: EventByAction<'press'>) => {
      console.log(`Pressed at ${event.x}, ${event.y}`);
      };

      mouse.on('press', handler);
      mouse.off('press', handler);
    • Registers a listener for a specific mouse event.

      Type Inference: This method uses TypeScript's type inference to provide accurate types for the event parameter based on the event name. For example:

      • For 'wheel' events, event.button is typed as 'wheel-up' | 'wheel-down' | 'wheel-left' | 'wheel-right'
      • For 'move' events, event.button is typed as 'none'
      • For 'drag' events, event.button excludes wheel buttons

      Type Parameters

      Parameters

      • event: T

        The name of the event to listen for.

      • listener: T extends "error" ? (error: Error) => void : ListenerFor<T>

        The callback function to execute when the event is triggered.

      Returns EventEmitter

      The event emitter instance.

      off to remove the listener

      const mouse = new Mouse();
      mouse.enable();

      // TypeScript knows event.button is a wheel button type here
      mouse.on('wheel', (event) => {
      console.log(event.button); // 'wheel-up' | 'wheel-down' | 'wheel-left' | 'wheel-right'
      });

      // TypeScript knows event.button is 'none' here
      mouse.on('move', (event) => {
      console.log(event.button); // 'none'
      });
    • Registers a one-time listener that automatically removes itself after the first event.

      Type Inference: This method uses the same type inference as on() to provide accurate types for the event parameter.

      Automatic Cleanup: The listener is automatically removed after the first invocation, preventing memory leaks and eliminating the need for manual cleanup code.

      Type Parameters

      Parameters

      • event: T

        The name of the event to listen for.

      • listener: T extends "error" ? (error: Error) => void : ListenerFor<T>

        The callback function to execute once when the event is triggered.

      Returns EventEmitter

      The event emitter instance.

      • on for persistent listeners
      • off to manually remove listeners
      const mouse = new Mouse();
      mouse.enable();

      // Listen for a single click
      mouse.once('click', (event) => {
      console.log('Got one click!', event);
      // Listener is automatically removed after this execution
      });

      // Wait for first wheel event
      mouse.once('wheel', (event) => {
      // TypeScript knows event.button is a wheel button type
      console.log(`Scrolled: ${event.button}`);
      });
      // Simplified one-time event handling
      // Before (manual cleanup required):
      const handler = (event) => {
      console.log('Got click', event);
      mouse.off('click', handler);
      // continue logic...
      };
      mouse.on('click', handler);

      // After (automatic cleanup):
      mouse.once('click', (event) => {
      console.log('Got click', event);
      // continue logic... listener already removed
      });
    • Pauses mouse event emission without disabling terminal mouse mode.

      This method temporarily stops the emission of mouse events while keeping the terminal mouse mode active. This is useful when you want to temporarily ignore mouse events without the overhead of disabling and re-enabling mouse tracking.

      Idempotent: Calling this method when already paused has no effect.

      No Terminal State Changes: Unlike disable, this method does not:

      • Send ANSI escape codes to the terminal
      • Modify the input stream's raw mode
      • Change the input stream encoding
      • Remove event listeners from the input stream

      Difference from disable():

      • pause(): Stops event emission only, terminal mouse mode remains active
      • disable(): Stops event emission AND deactivates terminal mouse mode

      Returns void

      • resume to resume event emission
      • disable to completely disable mouse tracking
      • isPaused to check if currently paused
      const mouse = new Mouse();
      mouse.enable();

      // Temporarily ignore mouse events during an operation
      mouse.pause();
      // ... perform operations that should not trigger mouse events
      mouse.resume();
      // Comparing pause() vs disable()
      const mouse = new Mouse();
      mouse.enable();

      // Using pause(): Fast, no terminal overhead
      mouse.pause();
      performQuickOperation();
      mouse.resume(); // Terminal mouse mode was never disabled

      // VS using disable(): Slower, terminal overhead
      mouse.disable();
      performQuickOperation();
      mouse.enable(); // Had to re-enable terminal mouse mode
    • Resumes mouse event emission without modifying terminal mouse mode.

      This method resumes the emission of mouse events after they were paused using pause. The terminal mouse mode remains active throughout.

      Idempotent: Calling this method when not paused has no effect.

      No Terminal State Changes: Unlike enable, this method does not:

      • Send ANSI escape codes to the terminal
      • Modify the input stream's raw mode
      • Change the input stream encoding
      • Add event listeners to the input stream

      Difference from enable():

      • resume(): Resumes event emission only, assumes terminal mouse mode is already active
      • enable(): Activates terminal mouse mode AND resumes event emission

      Returns void

      • pause to pause event emission
      • enable to completely enable mouse tracking
      • isPaused to check if currently paused
      const mouse = new Mouse();
      mouse.enable();

      // Temporarily ignore mouse events during an operation
      mouse.pause();
      // ... perform operations that should not trigger mouse events
      mouse.resume(); // Events will now be emitted again
      // Comparing resume() vs enable()
      const mouse = new Mouse();
      mouse.enable();

      // Pause and resume: Fast state change
      mouse.pause();
      performOperation();
      mouse.resume(); // No terminal overhead

      // VS disable and enable: Slower, re-enables terminal
      mouse.disable();
      performOperation();
      mouse.enable(); // Re-enables terminal mouse mode (ANSI codes, raw mode)
    • Returns an async generator that yields all mouse events. Each yielded value is an object containing the event type and the event data.

      Parameters

      • Optionaloptions: { latestOnly?: boolean; maxQueue?: number; signal?: AbortSignal }

        Configuration for the event stream.

        • OptionallatestOnly?: boolean

          If true, only the latest event is buffered. Defaults to false.

        • OptionalmaxQueue?: number

          The maximum number of events to queue. Defaults to 1000.

        • Optionalsignal?: AbortSignal

          An AbortSignal to cancel the async generator.

      Returns AsyncGenerator<MouseStreamEvent>

      An object with the event type and data.

    • Waits for a single click event and returns it.

      This is a convenience method that wraps the streaming API into a simple promise-based helper. It's useful for common interaction patterns like "wait for user to click anywhere".

      Timeout: The method will reject with a MouseError if the timeout is exceeded.

      Cancellation: The method can be cancelled early using an AbortSignal.

      Parameters

      • Optionaloptions: { signal?: AbortSignal; timeout?: number }

        Configuration options for the wait operation.

        • Optionalsignal?: AbortSignal

          An AbortSignal to cancel the operation early.

        • Optionaltimeout?: number

          Maximum time to wait in milliseconds. Defaults to 30000 (30 seconds).

      Returns Promise<XtermMouseEvent>

      A promise that resolves with the click event.

      If timeout is exceeded or operation is aborted.

      const mouse = new Mouse();
      mouse.enable();

      try {
      const click = await mouse.waitForClick();
      console.log(`Clicked at ${click.x}, ${click.y} with ${click.button}`);
      } catch (err) {
      if (err instanceof MouseError) {
      console.error('Timeout or error:', err.message);
      }
      } finally {
      mouse.disable();
      }
      // Wait with custom timeout
      const click = await mouse.waitForClick({ timeout: 5000 });
      // Cancel with AbortController
      const controller = new AbortController();
      setTimeout(() => controller.abort(), 1000);

      try {
      const click = await mouse.waitForClick({ signal: controller.signal });
      } catch (err) {
      if (err instanceof MouseError && err.message.includes('aborted')) {
      console.log('Wait cancelled');
      }
      }
    • Waits for any mouse input event and returns it.

      This is a convenience method that waits for any mouse event (press, release, click, drag, wheel, or move). Useful for "wait for any user interaction" patterns.

      Timeout: The method will reject with a MouseError if the timeout is exceeded.

      Cancellation: The method can be cancelled early using an AbortSignal.

      Parameters

      • Optionaloptions: { signal?: AbortSignal; timeout?: number }

        Configuration options for the wait operation.

        • Optionalsignal?: AbortSignal

          An AbortSignal to cancel the operation early.

        • Optionaltimeout?: number

          Maximum time to wait in milliseconds. Defaults to 30000 (30 seconds).

      Returns Promise<XtermMouseEvent>

      A promise that resolves with the first mouse event received.

      If timeout is exceeded or operation is aborted.

      const mouse = new Mouse();
      mouse.enable();

      try {
      const event = await mouse.waitForInput();
      console.log(`Got ${event.action} at ${event.x}, ${event.y}`);
      } catch (err) {
      if (err instanceof MouseError) {
      console.error('Timeout or error:', err.message);
      }
      } finally {
      mouse.disable();
      }
      // Wait with custom timeout
      const event = await mouse.waitForInput({ timeout: 5000 });
      // Use for "press any key to continue" style interaction
      console.log('Move mouse or click to continue...');
      await mouse.waitForInput();
      console.log('Continuing...');
    • Performs a detailed check of terminal mouse event support.

      This method provides more information than isSupported() by checking specific streams and returning the reason if support is not available.

      Use Cases:

      • Need detailed information about why mouse events aren't supported
      • Checking custom streams
      • Providing user-friendly error messages

      Parameters

      • Optionaloptions: Pick<MouseOptions, "inputStream" | "outputStream">

        Optional configuration with custom streams to check

        • inputStream

          The input stream to check (defaults to process.stdin)

        • outputStream

          The output stream to check (defaults to process.stdout)

      Returns string

      A result from SupportCheckResult indicating support status

      import { Mouse } from 'xterm-mouse';

      const result = Mouse.checkSupport();
      if (result === Mouse.SupportCheckResult.Supported) {
      console.log('Mouse events are supported!');
      } else if (result === Mouse.SupportCheckResult.NotTTY) {
      console.error('Not running in a terminal');
      } else if (result === Mouse.SupportCheckResult.OutputNotTTY) {
      console.error('Output is not a terminal');
      }
      // Check custom streams
      const result = Mouse.checkSupport({
      inputStream: myCustomStdin,
      outputStream: myCustomStdout,
      });
    • Checks if the current terminal environment supports mouse events.

      This is a convenience method that wraps checkSupport and returns a simple boolean. It checks if the provided streams (or process.stdin/ process.stdout by default) are TTYs.

      Use Cases:

      • Before creating a Mouse instance in environments that may not support TTY
      • To provide better error messages in CLI tools
      • To conditionally enable mouse features in applications
      • Checking custom streams before passing to Mouse constructor

      Note: For detailed error information (e.g., to distinguish between input and output stream issues), use checkSupport instead.

      Parameters

      • OptionalinputStream: ReadableStreamWithEncoding

        Optional input stream to check (defaults to process.stdin)

      • OptionaloutputStream: WriteStream

        Optional output stream to check (defaults to process.stdout)

      Returns boolean

      true if the terminal likely supports mouse events

      import { Mouse } from 'xterm-mouse';

      // Check default streams
      if (Mouse.isSupported()) {
      const mouse = new Mouse();
      mouse.enable();
      } else {
      console.log('Mouse events not supported in this environment');
      }
      // Check custom streams
      const customStdin = getCustomStdin();
      const customStdout = getCustomStdout();

      if (Mouse.isSupported(customStdin, customStdout)) {
      const mouse = new Mouse({
      inputStream: customStdin,
      outputStream: customStdout,
      });
      mouse.enable();
      }