Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

multi-key-handler

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multi-key-handler

A utility class for handling multiple key events and executing a callback function based on the key states.

  • 0.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
62
increased by51.22%
Maintainers
0
Weekly downloads
 
Created
Source

MultiKeyHandler

The MultiKeyHandler class allows you to handle multiple key combinations, including arrow keys, with customizable options for debugging and event handling.

Installation

You can install the package using npm:

npm install multi-key-handler

Usage

Create an instance of MultiKeyHandler and provide a callback function to execute when a key combination is pressed.

Example

import { MultiKeyHandler } from 'multi-key-handler';

const handler = new MultiKeyHandler((states) => {
  console.log('Current key states:', states);
}, {
  keys: 'asdf', // Listen for 'a', 's', 'd', 'f' keys
  arrows: true, // Enable arrow key handling
  debug: true,  // Enable debug mode
});

Callback Function

The callback function receives an object containing the current state of the keys being pressed. Each key's state is either true (pressed) or false (released).

(states) => {
  console.log(states);
  // Example output:
  // {
  //   a: true,
  //   s: false,
  //   d: true,
  //   f: false,
  //   up: false,
  //   down: true
  // }
}

API

Constructor

new MultiKeyHandler(callback, options);
  • callback: A function that will be called whenever a key is pressed or released.
  • options: An optional object to configure the handler:
    • keys: A string of keys to listen for.
    • arrows: A boolean indicating if the arrow keys should be included (true or false).
    • debug: A boolean to enable debug mode.

Methods

keymap (Getter)

Returns the currently configured keys as a string.

console.log(handler.keymap); // 'asdf'
keymap (Setter)

Sets the keys to listen for. The input should be a string.

handler.keymap = 'qwer';

Options

OptionTypeDefaultDescription
keysstring''The keys to listen for.
arrowsbooleanfalseWhether to include arrow keys.
debugbooleanfalseEnables debug mode.

Debug Mode

When debug mode is enabled, a table of the current key states will be printed to the console whenever a change occurs.

new MultiKeyHandler((states) => {}, { debug: true });

License

This project is licensed under the MIT License.

Keywords

FAQs

Package last updated on 29 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc