0.1.6-development • Published 9 months ago

mousekeyhook.js v0.1.6-development

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

This library is still in a very early stage and it's under heavy development, please expect bugs and errors.

MouseKeyHook.js

A Node.js wrapper for .NET GlobalMouseKeyHook

Install

npm install mousekeyhook.js

Usage

import {
  getGHotkeyServiceInstance,
  GlobalHotkeyService,
  isGHotkeyServiceRunning,
  KeyEvent, keyEventFromString,
  RespondType
} from 'mousekeyhook.js';

// Creates a new instance of GlobalHotkeyService
// or returns the existing instance if it is already running
const gHotkeyInstance: GlobalHotkeyService = getGHotkeyServiceInstance();

if (isGHotkeyServiceRunning()){
  gHotkeyInstance.onHotkeyEvent.push((event: KeyEvent) => {
    // event.type = RespondType.KEY_UP | RespondType.KEY_DOWN | RespondType.MOUSE_UP | RespondType.MOUSE_DOWN;
    // event.value = "Control + Alt + A"; (example)
    // Key enumeration follows https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.keys?view=net-5.0)
    console.log(event.type, event.value);

    if (event.type == RespondType.KEY_UP || event.type == RespondType.KEY_DOWN){
      console.log("Key pressed: " + event.value);
    } else if (event.type == RespondType.MOUSE_UP || event.type == RespondType.MOUSE_DOWN){
      console.log("Mouse button pressed: " + event.value);
    }

    // Same as onHotkeyEvent, but designed to be used for one-time hotkey event
    gHotkeyInstance.addTempKeyListener(()=>{});
    gHotkeyInstance.removeTempKeyListener();

    const myKeyEvent = new KeyEvent(RespondType.KEY_DOWN, "Control + Alt + A");

    if (event === myKeyEvent){
      console.log("My hotkey is pressed!");
    }
  });

  gHotkeyInstance.onProcessExit = (() => {
    console.log('Global hotkey service exited.');
  });

}

API

See Usage

Credit

GlobalMouseKeyHook