Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-keycodes

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-keycodes

Identify and work with keycodes on JavaScript

  • 1.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

js-keycodes

travis build codecov NPM version Downloads License

Identify and work with keyCodes on JavaScript

Install

npm i -S js-keycodes

Usage

First import the lib into your code:

import * as KeyCodes from 'js-keycodes';

Or individual functions:

import { isLetter, isNumber } from 'js-keycodes';
Verify if a keyCode is a letter
import { isLetter } from 'js-keycodes';

const myKeyCode = 90;
if (isLetter(myKeyCode)) {
  // Do stuff if your keyCode is a letter
}
Verify if ENTER is pressed on an event
import { keyCodes } from 'js-keycodes';

const handleAnEvent = (event) => {
  if (event.which === keyCodes.ENTER) {
    // Do your stuff here.
  }
}

Methods

KeyCodes.isNumber()

Return true if the keyCode is a number from 0 to 9.

KeyCodes.isLetter()

Return true if the keyCode is a letter from A to Z.

KeyCodes.isNavigation()

Return true if the keyCode represents an Arrow key like UP, DOWN, LEFT or RIGHT.

KeyCodes.isWhitespace()

Return true if the keyCode represent ENTER, SPACE or TAB

KeyCodes.isF1ToF12()

Return true if the keyCode represent any of the F buttons from F1 to F12

KeyCodes.keyCodeToString()

Return a string representation of the keyCode.

import { keyCodeToString } from 'js-keycodes';

const myLetter = keyCodeToString(90);
// -> 'Z'

const myNumber = keyCodeToString(50)
//  '2'
KeyCodes.getKeyNameFromKeyCode()

Return a string representation based on the programmatic name of the keyCode

import { getKeyNameFromKeyCode } from 'js-keycodes';

const myLetter = getKeyNameFromKeyCode(90);
// -> 'Z'

const myNumber = getKeyNameFromKeyCode(50);
// -> 'TWO'

const myKey = getKeyNameFromKeyCode(13);
// -> 'ENTER'

const myF = getKeyNameFromKeyCode(121);
// -> 'F10'

Available Key Codes

Usage

import { keyCodes } from 'js-keycodes';

keyCodes.ENTER === 13;
// -> true
keyCodes.F12 === 123;
// -> true

// Using with JQuery
$.click((event) => {
  if (event.keyCode === keyCodes.F1) {
    // do an amazing think!
  }
});
Key NameKey Code
ENTER13
ESC27
BACKSPACE8
TAB9
SHIFT16
CTRL17
ALT18
SPACE32
PAUSE19
PAGE_UP33
PAGE_DOWN34
END35
HOME36
LEFT37
UP38
RIGHT39
DOWN40
INSERT45
DELETE46
F1112
F2113
F3114
F4115
F5116
F6117
F7118
F8119
F9120
F10121
F11122
F12123
DOT190
DOT_NUMPAD110
COMA188
COMA_NUMPAD0
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
ZERO48
ONE49
TWO50
THREE51
FOUR52
FIVE53
SIX54
SEVEN55
EIGHT56
NINE57

Keywords

FAQs

Package last updated on 05 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc