Overview

The Hook Manager provides Windows-compatible hook functionality, allowing applications to intercept system events before they reach their destination. Hooks enable system-wide monitoring, debugging, and custom behavior modification.

Hook Types

The Hook Manager supports the following hook types:

  • WH_KEYBOARD: Keyboard input hooks
  • WH_MOUSE: Mouse input hooks
  • WH_CBT: Computer-Based Training hooks (window creation/destruction)
  • WH_CALLWNDPROC: Window procedure call hooks
  • WH_GETMESSAGE: Message retrieval hooks

Features

  • Global Hooks: System-wide event interception
  • Thread Hooks: Thread-specific event interception
  • Hook Chain: Multiple hooks can be installed for the same type
  • Hook Filtering: Hooks can prevent event processing

API

setWindowsHookEx(hookType, callback, hMod, threadId)

Installs a hook:

  • hookType - Hook type (WH_KEYBOARD, WH_MOUSE, etc.)
  • callback - Hook callback function
  • hMod - Module handle (optional)
  • threadId - Thread ID (0 for global hook)

Returns the hook ID.

unhookWindowsHookEx(hookId)

Uninstalls a hook:

  • hookId - Hook ID to remove

callHooks(hookType, data)

Invokes all hooks of a specific type:

  • hookType - Hook type to call
  • data - Hook data

Returns the result from the hook chain (non-zero stops processing).

Usage Example

const HookManager = require('./system/hooks');

const hookManager = new HookManager();

// Install a keyboard hook
const hookId = hookManager.setWindowsHookEx(
    'WH_KEYBOARD',
    (data) => {
        console.log('Key pressed:', data.keyCode);
        // Return 0 to continue processing, non-zero to block
        return 0;
    },
    null,
    0 // Global hook
);

// Later, remove the hook
hookManager.unhookWindowsHookEx(hookId);

Hook Processing

Hooks are processed in installation order:

  1. System calls callHooks() with hook type and data
  2. Hook Manager iterates through installed hooks of that type
  3. Each hook callback is invoked with the data
  4. If a hook returns non-zero, processing may stop
  5. Result is returned to the caller

Integration

The Hook Manager integrates with:

  • Input Manager: Keyboard and mouse hooks
  • Window Manager: Window creation/destruction hooks
  • Message System: Message processing hooks
  • GUI Integration: Initialized as part of the GUI system

Related Documentation