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

@oleksii-pavlov/keyboard

v0.2.0

Published

The JS/TS library to handle keyboard events

Readme

@oleksii-pavlov/keyboard

A flexible, TypeScript-first library for handling keyboard events and hotkey combinations in a framework-agnostic way. Create composable handlers for hotkey selectors like Ctrl+C, Alt+Enter, or Cmd+Shift+Z.

  • ✅ Declarative Hotkey definitions Ctrl+C, Ctrl+Alt+Enter
  • ✅ Pluggable into any environment (DOM, React, Vue, etc.)
  • ✅ Support for multiple named/anonymous handlers per hotkey
  • ✅ Built-in alias mapping (Cmd, Option, Esc etc.)
  • ✅ Use on keydown, keyup, or keypress — your choice

📦 Installation

npm install @oleksii-pavlov/keyboard

✨ Usage

Import

import { keyboard } from '@oleksii-pavlov/keyboard'

🧠 Selector Syntax

Selectors are simple strings like:

  • "Ctrl+S"
  • "Cmd+Shift+K"
  • "Alt+Enter"
  • "Plus" (matches the + key)
  • "Minus" (matches the - key)

The full list of aliases is:

| Alias | Normalized As | | ------------------------ | ------------- | | Ctrl, Control | ctrl | | Shift | shift | | Alt, Option | alt | | Meta, Cmd, Command | meta | | Plus | + | | Minus | - | | Esc, Escape | escape | | Enter, Return | enter | | Space | ' ' | | Up | arrowup | | Down | arrowdown | | Left | arrowleft | | Right | arrowright |


🔌 Framework-Agnostic Integration

The handlers you create are just pure callbacks that accept a KeyboardEvent — bind them wherever and however you like:

✅ Vanilla JavaScript

// create handler with Hotkey and callback
const handler = keyboard.setHandler('Ctrl+S', (e) => {
  e.preventDefault()
  alert('Saved!')
})

// apply handler wherever you need
window.addEventListener('keydown', handler) // or 'keyup', 'keypress'

✅ React

useEffect(() => {
  const handler = keyboard.setHandler('Ctrl+S', (e) => {
    e.preventDefault()
    alert('Saved from React!')
  })

  window.addEventListener('keydown', handler)
  return () => window.removeEventListener('keydown', handler)
}, [])

✅ Element level

import { keyboard } from '@oleksii-pavlov/keyboard'

const input = document.querySelector('input')!

// Handle Ctrl+Enter inside the input
const handler = keyboard.setHandler('Ctrl+Enter', (e) => {
  alert('Submitted from input!')
})

// apply handler for the input
input.addEventListener('keydown', handler)

🧩 API Overview

keyboard.setHandler(selector, callback)

Replaces all handlers for the given selector with a new anonymous one. Use this for global hotkeys to prevent conflicts.

keyboard.setHandler('Ctrl+S', (e) => {
  console.log('Saved!')
})

keyboard.addHandler(selector, callback)

Adds an anonymous handler without removing existing ones.


keyboard.setNamedHandler(selector, name, callback)

Sets a named handler (removing others for that selector/hotkey).

keyboard.setNamedHandler('Ctrl+Z', 'undo', (e) => {
  console.log('Undo!')
})

keyboard.addNamedHandler(selector, name, callback)

Adds a named handler to the selector.

keyboard.addNamedHandler('Ctrl+Z', 'customUndo', (e) => {
  console.log('Custom Undo (override behavior)')
})

keyboard.removeHandlers(selector)

Removes all handlers for the selector.


keyboard.removeNamedHandler(selector, name)

Removes a specific named handler from given selector.


keyboard.removeNamedHandlersForAllSelectors(name)

Removes a named handler across all selectors.


💡 Use Case: Multiple Named Handlers per Hotkey

const loggerHandler = keyboard.addNamedHandler('Ctrl+C', 'logger', (e) => {
  console.log('Ctrl+C pressed')
})

const clipboardHandler = keyboard.addNamedHandler('Ctrl+C', 'clipboard', (e) => {
  console.log('Intercepted copy to clipboard')
})

Now bind it once:

window.addEventListener('keydown', loggerHandler)
window.addEventListener('keydown', clipboardHandler)

Output when pressing Ctrl+C

Ctrl+C pressed
Intercepted copy to clipboard

Remove one handler by name:

keyboard.removeNamedHandler('Ctrl+C', 'logger')

Output now:

Intercepted copy to clipboard

Remove all handlers:

keyboard.removeHandlers('Ctrl+C')

🧪 Advanced: Dynamic Registration

keyboard.addNamedHandler('Cmd+K', 'openSearch', () => {
  console.log('Open search')
})

keyboard.addNamedHandler('Cmd+K', 'logger', () => {
  console.log('Cmd+K triggered')
})

keyboard.addNamedHandler('Cmd+W', 'logger', () => {
  console.log('Cmd+W triggered')
})

// removes from both Cmd+K and Cmd+W
keyboard.removeNamedHandlersForAllSelectors('logger')

🔍 Exports

From the main entrypoint:

  • keyboard — instance of Keyboard. Manages handlers creating and deleting

Also includes:

  • keyAliases, keyFilters - mappers used to differentiate hotkeys
  • Types:
    • Selector = string - for hotkeys. Example: "Ctrl+K"
    • Key = string - for single keys. Example: "K"
    • Handler = [ Name, Callback ] - entry for selector handlers
    • Name = string | number | symbol - represents the name of the handler
    • Filter = (e: KeyboardEvent) => boolean - checks that specific key is pressed
    • Callback = (e: KeyboardEvent) => void - generated callback to be applied by client code

✅ Compatible Environments

  • Browser (DOM events)
  • React / Vue / Svelte / SolidJS
  • Vanilla JS or TS projects
  • Electron / Tauri / Webview apps
  • Node.js with simulated events (for testing)