Skip to content

asnow-io/keycodejs

Repository files navigation

KeycodeJS

An enum of frequent keycodes.

Install

npm install @asnow-io/keycodejs

Usage

import { Keycode, wasPressed } from '@asnow-io/keycodejs';

const textField = document.getElementById('#text-field');

function sendMessage() {
  // ...
}

textField.addEventListener('keydown', (e) => {
  if (wasPressed(e, Keycode.ENTER)) {
    e.preventDefault();
    sendMessage();
  }

  // `wasPressed(e, Keycode.ENTER)`
  // equals
  // `e.code === Keycode.ENTER`
})

Typings

enum Keycode {
  BACKSPACE = 'Backspace',
  TAB = 'Tab',
  ENTER = 'Enter',
  // and others
}

function wasPressed(e: KeyboardEvent, key: string | string[]): boolean;