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 ๐Ÿ™

ยฉ 2024 โ€“ย Pkg Stats / Ryan Hefner

keyboard-actions

v2.0.1

Published

Small package, that allow you to assign actions to keyboard buttons. to make user's experience alot easier and better ๐ŸŽ‰

Downloads

59

Readme

KeyboardActions

KeyboardActions is a lightweight JavaScript utility that allows you to easily define custom keyboard shortcuts and manage key events in your web applications. With features like customizable key combinations, and action hooks, this package provides a flexible way to handle keyboard interactions.

Installation

Install the package via npm:

npm install keyboard-actions

Features

  • Custom Key Combinations: Define actions for specific key sequences.
  • Hooks: Pre-action and post-action hooks to run custom logic before or after the key action.
  • Automatic Key Ordering: Ensure that key combinations are recognized regardless of the order of key presses.
  • Event Management: Easily add or remove event listeners.
  • Logging: Optionally log key combinations to the console for debugging.

Usage

Basic Setup

First, import and initialize the KeyboardActions class:

const KeyboardActions = require('keyboard-actions');

const keyboardActions = new KeyboardActions();

OR

<!-- Load the script -->
<script src="https://unpkg.com/keyboard-actions@latest"></script>

<script>
// start using it
const keyboardActions = new KeyboardActions();

</script>

Starting and Stopping the Keyboard Listener

You can start listening to key events by calling the start method. To stop listening, use the stop method.

keyboardActions.start(); // Begin listening for key events
keyboardActions.stop();  // Stop listening for key events

// to read the running status
console.log(keyboardActions.isRunning);

Adding a Key Action

To add a custom action for a specific key or key combination, use the addKeyAction method:

Defining key action can be in this ways

  • Array -> ["Control" + "S"]
  • String -> "Control + S"

Also Keys are not case sensitive, its tolerance with upper and lower cases, but make sure to write the right name

NOTE: you can not the name by just turn on the Logging

keyboardActions.addKeyAction('Control + S', (event) => {
  console.log('Save command triggered!');
  // Your custom save logic here
});

You can also add actions for single keys:

keyboardActions.addKeyAction('Escape', (event) => {
  console.log('Escape key pressed!');
});

Clearing a Key Action

If you need to remove a specific key action, use the clearKeyAction method:

keyboardActions.clearKeyAction('Control + S');

Configuration Options

The KeyboardActionsConfig class provides several options to customize the behavior of the keyboard listener.

Default Configuration

const config = {
  element: document,           // The DOM element to attach the event listeners to
  logKeys: true,               // Whether to log key combinations to the console
  keepDefault: true,          // Prevent default browser behavior for key events
  isAutoKeyOrdering: true,     // Automatically order key combinations
  preActionHook: () => {},     // Function to run before each key action
  postActionHook: () => {},    // Function to run after each key action
};

Customizing Configuration

You can modify the configuration by accessing the config property of the KeyboardActions instance:

keyboardActions.config.logKeys = false;  // Disable logging
keyboardActions.config.keepDefault = true; // Allow default browser behavior
keyboardActions.config.preActionHook = () => {
  console.log('Action is about to be executed!');
};
keyboardActions.config.postActionHook = () => {
  console.log('Action executed!');
};

Example: Implementing a Simple Shortcut

Here's an example of how you can implement a simple keyboard shortcut using KeyboardActions:

const KeyboardActions = require('keyboard-actions');

const keyboardActions = new KeyboardActions();

keyboardActions.addKeyAction('Control + N', (event) => {
  console.log('New document command triggered!');
  // Your logic to create a new document
});

keyboardActions.addKeyAction('Control + S', (event) => {
  console.log('Save command triggered!');
  // Your logic to save the current document
});

keyboardActions.start();

Example: Using Hooks and Auto Key Ordering

const keyboardActions = new KeyboardActions();

// Custom pre-action and post-action hooks
keyboardActions.config.preActionHook = () => console.log('Before action');
keyboardActions.config.postActionHook = () => console.log('After action');

// Enable automatic key ordering
keyboardActions.config.isAutoKeyOrdering = true;

keyboardActions.addKeyAction('N + Control', () => {
  console.log('This will work even if N is pressed before Control');
});

keyboardActions.start();

Contributing

Contributions are welcome! Please submit a pull request or open an issue if you have any suggestions or find any bugs.

License

This project is licensed under the MIT License - see the LICENSE file for details.