• Stars
    star
    2,053
  • Rank 21,543 (Top 0.5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

KeyboardJS


Note Please Note: I've create a new libary called Keystrokes which serves the same purpose as KeyboardJS, but is more refined, and takes full advantage of modern browsers. If you are considering using KeyboardJS in a new project, I recommend checking out Keystrokes first.


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. If you're not using a build system like webpack, simply add keyboard.js or keyboard.min.js from the dist folder in this repo to your project via a script tag.

npm install keyboardjs

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

Setting up bindings is easy

keyboardJS.bind('a', (e) => {
  console.log('a is pressed');
});
keyboardJS.bind('a + b', (e) => {
  console.log('a and b is pressed');
});
keyboardJS.bind('a + b > c', (e) => {
  console.log('a and b then c is pressed');
});
keyboardJS.bind(['a + b > c', 'z + y > z'], (e) => {
  console.log('a and b then c or z and y then z is pressed');
});
keyboardJS.bind('', (e) => {
  console.log('any key was pressed');
});
//alt, shift, ctrl, etc must be lowercase
keyboardJS.bind('alt + shift > a', (e) => {
  console.log('alt, shift and a is pressed');
});

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

keydown vs a keyup

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

Prevent keydown repeat

keyboardJS.bind('a', (e) => {
  // this will 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', (e) => {});
  keyboardJS.bind('b', (e) => {});
  keyboardJS.bind('c', (e) => {});

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

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

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

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

  // you can also set up handlers for a context without losing the current context
  keyboardJS.withContext('bar', ()  =>{
    // these will execute in the bar context
    keyboardJS.bind('7', (e) => {});
    keyboardJS.bind('8', (e) => {});
    keyboardJS.bind('9', (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();

More Repositories

1

LucidJS

LucidJS is a library boasting an EventEmitter than supports emitter piping, flags, event scope, and a collection of useful meta events
JavaScript
365
star
2

OriginJS

A Client-side router designed to build robust web applications.
JavaScript
53
star
3

Red-Locomotive

A Javascript engine for Complex 2D and 2.5D games.
JavaScript
47
star
4

Keystrokes

Keystrokes as an easy to use library for binding functions to keys and key combos. It can be used with any TypeScript or JavaScript project, even in non-browser environments.
TypeScript
42
star
5

Relign

relign is a promise based control flow library heavily inspired by async
JavaScript
32
star
6

node-trees

Library contains quad tree class that will grow automatically as data is inserted
JavaScript
14
star
7

Theta

Ο΄ - Realtime API Framework for Node.js
TypeScript
7
star
8

blackbox

Blackbox is a simple logger with support for contextual information, and multiple write targets
Go
5
star
9

Guard

Guard is a type checker that aims to make type checking function arguments easier and more readable.
JavaScript
5
star
10

Merlin

JavaScript
4
star
11

Absinthe

Absinthe - Take your Gin and go Nats. Absinthe is a framework for extending the gin framework across Nats. It also introduces RPC over Nats for service to service communication.
Go
4
star
12

Milieu

Config loader for node
JavaScript
3
star
13

AptJS

An apt solution for control flow JavaScript. AptJS offers parallel and serial execution and mapping in a painless way. It also offers super slick error handling.
JavaScript
3
star
14

Quil

A simple logger for Rust projects
Rust
3
star
15

Tritium

Tritium is a proxy built with the goal of being ultra fast, easy to use, and feature rich
Rust
3
star
16

Unison

Unison is a easy to use config loader which can collect configuration from flags, environment variables, and configuration files.
Rust
3
star
17

Task-Kit

Task Kit is a Rust crate that makes it easy to build asynchronous and concurrent systems.
Rust
2
star
18

Hull

Hull - Give your App some structural integrity.
JavaScript
2
star
19

Mongoose-Count-And-Find

JavaScript
2
star
20

HRL-for-cakePHP

This is the Hierarchical Resource Loader or HRL for cakePHP 1.3. It managing CSS and JavaScript dependencies hierarchical breezy.
PHP
2
star
21

planeid

identify planes
JavaScript
1
star
22

Merlin-Schema

JavaScript
1
star
23

CubeStory

Java
1
star
24

Talisman

JavaScript
1
star
25

Delta

Rust
1
star
26

Silicate

Silicate is an MVC framework for Node inspired by Ruby on Rails
JavaScript
1
star
27

Craigslist-Tracker

JavaScript
1
star
28

RobertWHurst

Config files for my GitHub profile.
1
star
29

Neodymium

JavaScript
1
star