0.9.2 • Published 8 years ago

keypress-combination v0.9.2

Weekly downloads
11
License
MIT
Repository
github
Last release
8 years ago

Keypress combination

NPM Version Circle CI Coverage Status Dependency Status NPM Downloads

Attach listeners to keypress combinations like CTRL-1. Lightweight and well documented.

// Keycode for CTRL is 17, keycode for 1 is 49
emitter.addListener(17, 49, function() {
  console.log("Pressed CTRL-1");
});

See the demo at jsfiddle.net/pushtell/164prhaz.

Please ★ on GitHub!

Installation

npm install keypress-combination

Usage

Try it on JSFiddle

var KeypressCombinationEmitter = require("keypress-combination");

var emitter = new KeypressCombinationEmitter();

// CTRL Keycode: 17
// 1 Keycode: 49
var subscription = emitter.addListener(17, 49, function() {
  console.log("Pressed CTRL-1");
});

// Remove the callback when no longer needed.
subscription.remove();

API Reference

KeypressCombinationEmitter

Emitter responsible for coordinating combination keypress events.

constructor

Create a new emitter.

  • Return Type:
  • Parameters:
    • No parameters.

.addListener(keyCode, [keyCode, ...] callback)

Add an event listener

  • Return Type:
  • Parameters:
    • keyCode - Character keycode. See the keycodes section for more information.
      • Required
      • Type: number
      • Example: 49
    • callback - Callback function.
      • Required
      • Type: function
      • Example: function() { console.log("Press") }

Subscription

.remove()

  • Return Type: No return value
  • Parameters:
    • No parameters.

Keycodes

Note that keycodes are not fully standardized across browsers.

KeyCode
Backspace8
Tab9
Enter13
Shift16
Ctrl17
Alt18
Pause/Break19
Caps Lock20
Escape27
(space)32
Page Up33
Page Down34
End35
Home36
Left Arrow37
Up Arrow38
Right Arrow39
Down Arrow40
Insert45
Delete46
048
149
250
351
452
553
654
755
856
957
A65
B66
C67
D68
E69
F70
G71
H72
I73
J74
K75
L76
M77
N78
O79
P80
Q81
R82
S83
T84
U85
V86
W87
X88
Y89
Z90
Left Window Key91
Right Window Key92
Select Key93
Numpad 096
Numpad 197
Numpad 298
Numpad 399
Numpad 4100
Numpad 5101
Numpad 6102
Numpad 7103
Numpad 8104
Numpad 9105
Multiply106
Add107
Subtract109
Decimal Point110
Divide111
F1112
F2113
F3114
F4115
F5116
F6117
F7118
F8119
F9120
F10121
F11122
F12123
Num Lock144
Scroll Lock145
Semi-Colon186
Equal Sign187
Comma188
Dash189
Period190
Forward Slash191
Grave Accent192
Open Bracket219
Back Slash220
Close Braket221
Single Quote222

Tests

Browser Coverage

Karma tests are performed on Browserstack in the following browsers:

  • IE 9, Windows 7
  • IE 10, Windows 7
  • IE 11, Windows 7
  • Opera (latest version), Windows 7
  • Firefox (latest version), Windows 7
  • Chrome (latest version), Windows 7
  • Safari (latest version), OSX Yosemite
  • Mobile Safari (latest version), iPhone 6, iOS 8.3

Please let us know if a different configuration should be included here.

Running Tests

Locally:

npm test

On Browserstack:

BROWSERSTACK_USERNAME=YOUR_USERNAME BROWSERSTACK_ACCESS_KEY=YOUR_ACCESS_KEY npm test