Convert Figma logo to code with AI

dmauro logoKeypress

A keyboard input capturing utility in which any key can be a modifier key.

3,185
313
3,185
35

Top Related Projects

11,641

Simple library for handling keyboard shortcuts in Javascript

➷ A robust Javascript library for capturing keyboard input. It has no dependencies.

A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.

A tiny (~650 B) & modern library for keybindings.

A JavaScript library for binding keyboard combos without the pain of key codes and key combo conflicts.

Quick Overview

Keypress is a lightweight JavaScript library for capturing keyboard input. It provides a simple and flexible way to handle keyboard events, including support for key combinations and sequences, making it easier to implement keyboard shortcuts and controls in web applications.

Pros

  • Easy to use with a clean, intuitive API
  • Supports complex key combinations and sequences
  • Lightweight with no dependencies
  • Works across different browsers and platforms

Cons

  • Not actively maintained (last update was in 2016)
  • Limited documentation and examples
  • May not support some newer keyboard features or events
  • No built-in TypeScript support

Code Examples

Basic key binding:

var listener = new window.keypress.Listener();
listener.simple_combo("shift s", function() {
    console.log("You pressed shift and s");
});

Binding a sequence of keys:

listener.sequence_combo("up up down down left right left right b a enter", function() {
    console.log("Konami code activated!");
});

Using meta keys and preventing default behavior:

listener.simple_combo("meta s", function(e) {
    console.log("You pressed cmd+s or ctrl+s");
    e.preventDefault();
    return false;
});

Getting Started

  1. Include the Keypress library in your HTML file:
<script src="https://cdnjs.cloudflare.com/ajax/libs/keypress/2.1.5/keypress.min.js"></script>
  1. Create a new listener and start binding keys:
var listener = new window.keypress.Listener();

listener.simple_combo("ctrl c", function() {
    console.log("You pressed ctrl+c");
});

listener.sequence_combo("up down left right", function() {
    console.log("You entered the sequence!");
});
  1. You can now use the library to handle keyboard events in your web application.

Competitor Comparisons

11,641

Simple library for handling keyboard shortcuts in Javascript

Pros of Mousetrap

  • Smaller file size and lighter weight (4.5KB vs 16KB for Keypress)
  • Broader browser compatibility, including older versions of IE
  • More active development and maintenance

Cons of Mousetrap

  • Less extensive documentation compared to Keypress
  • Fewer advanced features, such as key sequence detection

Code Comparison

Mousetrap:

Mousetrap.bind('ctrl+s', function(e) {
    e.preventDefault();
    // Save functionality
});

Keypress:

var listener = new window.keypress.Listener();
listener.simple_combo("ctrl s", function() {
    // Save functionality
});

Both libraries offer similar functionality for basic key binding, but Keypress provides more options for complex combinations and sequences. Mousetrap's syntax is slightly more concise, while Keypress offers a more object-oriented approach.

Mousetrap is generally easier to set up and use for simple key bindings, making it a good choice for projects that don't require advanced keyboard input handling. Keypress, on the other hand, offers more flexibility and power for complex keyboard interactions, but at the cost of a larger file size and potentially steeper learning curve.

➷ A robust Javascript library for capturing keyboard input. It has no dependencies.

Pros of hotkeys-js

  • Lightweight and simple to use, with a smaller footprint than Keypress
  • Supports a wider range of keyboard events, including key combinations and sequences
  • More actively maintained with recent updates and bug fixes

Cons of hotkeys-js

  • Less comprehensive documentation compared to Keypress
  • Fewer advanced features for complex key mapping scenarios
  • May require more setup for certain use cases

Code Comparison

Keypress:

var listener = new window.keypress.Listener();
listener.simple_combo("shift s", function() {
    console.log("You pressed shift and s");
});

hotkeys-js:

import hotkeys from 'hotkeys-js';
hotkeys('shift+s', function(event, handler) {
    console.log("You pressed shift and s");
});

Both libraries offer similar functionality for basic key combinations, but hotkeys-js uses a more concise syntax. Keypress requires creating a listener object, while hotkeys-js allows direct binding of key combinations to functions. The hotkeys-js approach may be more intuitive for developers familiar with event-driven programming.

Overall, hotkeys-js is a more modern and actively maintained library, offering a simpler API for most use cases. However, Keypress may still be preferred for projects requiring more advanced key mapping features or those already using it in legacy codebases.

A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.

Pros of Keymaster

  • Smaller file size (3KB minified and gzipped)
  • Supports key sequences and combos out of the box
  • More actively maintained with recent updates

Cons of Keymaster

  • Less extensive documentation compared to Keypress
  • Fewer advanced features like key filtering and mouse support
  • Limited support for non-US keyboard layouts

Code Comparison

Keymaster:

key('⌘+r, ctrl+r', function() {
  alert('stopped reload!');
  return false;
});

Keypress:

var listener = new window.keypress.Listener();
listener.simple_combo("meta r", function() {
  alert('stopped reload!');
  return false;
});

Both libraries offer similar functionality for binding key combinations, but Keymaster's syntax is slightly more concise. Keypress provides a more object-oriented approach with its Listener class, while Keymaster uses a global key function.

Keymaster is generally simpler to use for basic key binding scenarios, while Keypress offers more advanced features and customization options at the cost of a larger file size and slightly more complex syntax.

A tiny (~650 B) & modern library for keybindings.

Pros of tinykeys

  • Significantly smaller bundle size (less than 1KB gzipped)
  • Modern JavaScript with ES6 module support
  • Simpler API for basic key binding scenarios

Cons of tinykeys

  • Less feature-rich compared to Keypress
  • Limited support for complex key combinations
  • No built-in support for key sequences or combos

Code Comparison

Keypress:

var listener = new window.keypress.Listener();
listener.simple_combo("shift s", function() {
    console.log("You pressed shift and s");
});

tinykeys:

import tinykeys from "tinykeys";

tinykeys(window, {
  "Shift+S": () => {
    console.log("You pressed shift and s");
  },
});

Summary

Tinykeys is a lightweight alternative to Keypress, offering a modern and simplified approach to key binding. It's ideal for projects where bundle size is a concern and basic key binding functionality is sufficient. However, Keypress provides more advanced features and greater flexibility for complex key combinations and sequences. The choice between the two depends on the specific requirements of your project and the level of key binding complexity needed.

A JavaScript library for binding keyboard combos without the pain of key codes and key combo conflicts.

Pros of KeyboardJS

  • More comprehensive API with support for key sequences and combos
  • Better cross-browser compatibility, especially for non-US keyboard layouts
  • Active development and maintenance

Cons of KeyboardJS

  • Slightly larger file size
  • May have a steeper learning curve for simpler use cases

Code Comparison

KeyboardJS:

keyboard.bind('ctrl + shift + a', function(e) {
  console.log('Ctrl+Shift+A pressed');
});

Keypress:

var listener = new window.keypress.Listener();
listener.simple_combo("ctrl shift a", function() {
  console.log('Ctrl+Shift+A pressed');
});

Key Differences

  • KeyboardJS offers more flexibility in defining key combinations
  • Keypress has a simpler API for basic key binding
  • KeyboardJS provides better support for international keyboards
  • Keypress is lighter-weight but has less active development

Use Cases

  • Choose KeyboardJS for complex keyboard interactions or international support
  • Opt for Keypress for simpler projects or when minimizing file size is crucial

Community and Support

  • KeyboardJS has more recent updates and a larger community
  • Keypress has a solid foundation but fewer recent contributions

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Keypress

Version 2.1.5

Keypress is a robust keyboard input capturing Javascript utility focused on input for games. For details and documentation, please visit http://dmauro.github.io/Keypress/

If you're using Keypress with Meteor, see the Meteor notes.

Copyright 2016 David Mauro
released under the Apache License, version 2.0

What's new

2.1.5

2.1.4

2.1.3

2.1.2

  • Added some simple jQuery proofing so you can pass in the result of a jQuery selector into the Listener's constructor. Thanks to mallocator for the request. Issue #89
  • Changed the default behavior of how sequence combos behavior. Most people probably would have considered this a bug, so I'm not considering this API breaking. Thanks to ronnyek for pointing the problem out in Issue #68.
  • Bower file cleaned up thanks to kkirsche. Pull request #97
  • Keys in FF/Gecko - and = fixed thanks to deanputney. Pull request #95

2.1.1

2.1.0

2.0.3

  • Fixed a bug unregistering combos using arrays of keys
  • Added ie8 compatibility shim. Thanks to barrkel. Issue #41
  • Fixed a bug targetting the semicolon key in Firefox. Thanks to mikekuehn.
  • Added commonJS module support. Issue #45

2.0.2

  • Fixed a bug that prevented combos from unregistering, and updated the docs for how to unregister properly. Thanks to pelly and g00fy-. Issue # 34.
  • Added AMD support. Issue #37.

2.0.1

  • Fixed a big ole bug with meta/cmd combos. Thanks to lexey111. Issue #29.
  • Fixed a bug with the Windows key being released on Windows systems. Issue #27.

2.0.0

  • Keypress now has a listener class that must be instantiated. The functions that were previously in the global window.keypress object are now public methods of the window.keypress.Listener class.
  • Each instance of a Keypress listener can be bound to a DOM element by passing in the element to the listener's constructor.
  • Combos now default to being ordered (the property is now called is_unordered and is false by default).
  • Combos' handlers preventDefault unless the handler returns true.
  • The "combo" public method is now called "simple_combo".
  • The basic combo helpers for simple, counting and sequence combos no longer have a third prevent_default optional parameter.
  • Debugging console logs can be enabled by setting keypress.debug to true.
  • All key event callbacks send a third argument specifying whether the event is firing again automatically because the key has remained pressed down.

1.0.9

  • Fix escape key bug. Issue #17.
  • Fix unregister bug. Issue #24.

1.0.8

  • Ensure that on_release is called for all combos, not just counting combos.
  • Fix bug that was causing is_ordered to be ignored.
  • Fixed an edge case for a counting combo's count not being reset.
  • Improve how key events are bound

1.0.7

  • Fixed combo matching to prevent performance issues as more keys are pressed.

1.0.6

  • Fixed a bug with exclusive combos not properly excluding each other in some cases.
  • Feature added to allow for combos that do not fire if unrelated keys are also pressed ("is_solitary" boolean).

TODO

  • Put negative edge in sequences.
  • See if we can do away with keyup_fired and count properties.

NPM DownloadsLast 30 Days