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

@andrewstart/keyboardjs

v2.4.1

Published

A library for binding to keys and key combos without the pain of key codes and key combo conflicts.

Downloads

3

Readme

KeyboardJS

Build Status NPM Version Downloads This Week License

KeyboardJS is a library for use in the browser (node.js compatible). It Allows developers to easily setup key bindings. Use key combos to setup complex bindings. KeyboardJS also provides contexts. Contexts are great for single page applications. They allow you to scope your bindings to various parts of your application. Out of the box keyboardJS uses a US keyboard locale. If you need support for a different type of keyboard KeyboardJS provides custom locale support so you can create with a locale that better matches your needs.

KeyboardJS is available as a NPM module for use with browserify (or in node.js). If you don't use browserify you can simply include keyboard.js or keyboard.min.js from the dist folder in this repo. These files are UMD wrapped so they can be used with or without a module loader such as requireJS.

npm install keyboardjs

Note that all key names can be found in ./locales/us.js.

If you're looking for the previous v1.x.x release of KeyboardJS you can find it here.

Setting up bindings is easy

keyboardJS.bind('a', function(e) {
  console.log('a is pressed');
});
keyboardJS.bind('a + b', function(e) {
  console.log('a and b is pressed');
});
keyboardJS.bind('a + b > c', function(e) {
  console.log('a and b then c is pressed');
});
keyboardJS.bind(['a + b > c', 'z + y > z'], function(e) {
  console.log('a and b then c or z and y then z is pressed');
});
keyboardJS.bind('', function(e) {
  console.log('any key was pressed');
});

// keyboardJS.bind === keyboardJS.on === keyboardJS.addListener

keydown vs a keyup

keyboardJS.bind('a', function(e) {
  console.log('a is pressed');
}, function(e) {
  console.log('a is released');
});
keyboardJS.bind('a', null, function(e) {
  console.log('a is released');
});

Prevent keydown repeat

keyboardJS.bind('a', function(e) {
  // this function will once run once even if a is held
  e.preventRepeat();
  console.log('a is pressed');
});

Unbind things

keyboardJS.unbind('a', previouslyBoundHandler);
// keyboardJS.unbind === keyboardJS.off === keyboardJS.removeListener

Using contexts


  // these will execute in all contexts
  keyboardJS.bind('a', function(e) {});
  keyboardJS.bind('b', function(e) {});
  keyboardJS.bind('c', function(e) {});

  // these will execute in the index context
  keyboardJS.setContext('index');
  keyboardJS.bind('1', function(e) {});
  keyboardJS.bind('2', function(e) {});
  keyboardJS.bind('3', function(e) {});

  // these will execute in the foo context
  keyboardJS.setContext('foo');
  keyboardJS.bind('x', function(e) {});
  keyboardJS.bind('y', function(e) {});
  keyboardJS.bind('z', function(e) {});

  // if we have a router we can activate these contexts when appropriate
  myRouter.on('GET /', function(e) {
    keyboardJS.setContext('index');
  });
  myRouter.on('GET /foo', function(e) {
    keyboardJS.setContext('foo');
  });

  // you can always figure out your context too
  var contextName = keyboardJS.getContext();

  // you can also set up handlers for a context without losing the current context
  keyboardJS.withContext('bar', function() {
    // these will execute in the bar context
    keyboardJS.bind('7', function(e) {});
    keyboardJS.bind('8', function(e) {});
    keyboardJS.bind('9', function(e) {});
  });

pause, resume, and reset


// the keyboard will no longer trigger bindings
keyboardJS.pause();

// the keyboard will once again trigger bindings
keyboardJS.resume();

// all active bindings will released and unbound,
// pressed keys will be cleared
keyboardJS.reset();

pressKey, releaseKey, and releaseAllKeys


// pressKey
keyboardJS.pressKey('a');
// or
keyboardJS.pressKey(65);

// releaseKey
keyboardJS.releaseKey('a');
// or
keyboardJS.releaseKey(65);

// releaseAllKeys
keyboardJS.releaseAllKeys();

watch and stop

// bind to the window and document in the current window
keyboardJS.watch();

// or pass your own window and document
keyboardJS.watch(myDoc);
keyboardJS.watch(myWin, myDoc);

// or scope to a specific element
keyboardJS.watch(myForm);
keyboardJS.watch(myWin, myForm);

// detach KeyboardJS from the window and document/element
keyboardJS.stop();