npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@coffic/key-listener

v1.0.4

Published

macOS系统按键事件监听器

Readme

@coffic/key-listener

English 简体中文

A global keyboard event listener for macOS.

Features

  • Listen to all keyboard events on macOS (both in-app and system-wide)
  • Support all types of keys (regular keys and modifier keys)
  • Implemented with native modules for high performance and stability
  • Simple event interface for easy integration
  • Smart handling of repeated events, triggering only once per keypress

Event Triggering Mechanism

This listener implements an intelligent event triggering mechanism:

  1. Single Trigger: Each key triggers an event only once when pressed, regardless of whether it's a regular key or a modifier key (like Command, Shift, etc.)
  2. Hold Handling: Holding down a key does not trigger repeated events
  3. Reset Mechanism: A new event is only triggered after releasing and pressing the key again
  4. Unified Processing: All types of keys are handled with the same logic, ensuring consistent and predictable behavior

This mechanism is particularly suitable for:

  • Hotkey listening
  • Global key triggers
  • Keyboard statistics and analysis
  • Keyboard event recording

Installation

npm install @coffic/key-listener

System Requirements

  • Operating System: macOS only
  • Node.js: >= 14.0.0
  • Build Tools: Xcode Command Line Tools (for native module compilation)

Basic Usage

import { KeyListener } from '@coffic/key-listener';

// Create listener instance
const listener = new KeyListener();

// Listen for keyboard events
listener.on('keypress', (event) => {
  console.log('Key event:', {
    keyCode: event.keyCode  // Key code
  });
});

// Start the listener (returns Promise)
listener.start().then(success => {
  if (success) {
    console.log('Listener started');
  } else {
    console.error('Failed to start listener');
  }
});

// ... Other application logic ...

// Stop listening (when no longer needed)
listener.stop();

API Reference

KeyListener Class

Constructor

new KeyListener()

Creates a new keyboard event listener instance.

Methods

start(): Promise<boolean>

Starts the listener. Returns a Promise that resolves to a boolean indicating success.

stop(): boolean

Stops the listener. Returns a boolean indicating success.

isListening(): boolean

Gets the current state of the listener. Returns a boolean indicating if it's listening.

Events

keypress

Triggered when a key event is detected. The event object contains:

  • keyCode: number - The key code of the pressed key
listener.on('keypress', (event) => {
  // Handle key event
});

Common Key Code Reference

  • 54: Right Command key
  • 55: Left Command key
  • 56: Left Shift key
  • 57: Caps Lock key
  • 58: Left Option key
  • 59: Left Control key
  • 60: Right Shift key
  • 61: Right Option key
  • 62: Right Control key
  • 63: Function key

For more key codes, please refer to example code or documentation.

Important Notes

  1. Platform Limitation: This package only works on macOS and will not function on other platforms (though it won't cause errors).

  2. Permission Requirements: Your application needs Accessibility permission to use this listener. macOS will prompt users to grant permission on first use.

  3. Application Packaging: When packaging your application with Electron or other tools, ensure proper inclusion of native module files.

  4. Event Handling:

    • Each key triggers an event only once when pressed
    • Holding a key won't trigger repeated events
    • New events require releasing and pressing the key again
    • This behavior applies to all key types, including modifier keys

Troubleshooting

Listener Won't Start

  • Check if you're running on macOS
  • Ensure Accessibility permission is granted
  • Check console for error messages

Compilation Errors

If you encounter compilation errors during installation:

  • Ensure Xcode Command Line Tools are installed: xcode-select --install
  • Verify Node.js version compatibility
  • Try rebuilding native modules with npm rebuild

License

MIT

Contributing

Issues and Pull Requests are welcome.