0.0.71 • Published 5 months ago

keyskey v0.0.71

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

KeysKey

NPM Version NPM Install Size Test CI

Provides the key to your keys.

Clean and readable interface for OS agnostic Keyboard mappings without the usual quirks.

https://www.npmjs.com/package/keyskey

Motivation

Purpose: Streamlines key event handling in web development.

Use Cases: For web games, HTML Canvas, and global key presses in Electron-like web-based software.

Current Issue: At the time when I started to need this, existing libraries were not user-friendly and did not produce readable code.

Solution: Provides a clear, readable method to detect specific key presses, simplifying cross-platform key event management.

Demo

Demo

Documentation

Since this library uses TypeScript just import and explore by letting your IntelliSense guide you.

Basic Usage

Get the event from keydown, keyup, keypress and populate it as the first argument always:

import KeysKey from "keyskey";

window.onkeydown = (event) => {
  const result = KeysKey.Is(event, "A");
  // assuming the letter A has been pressed
  console.log(result); // [ 'A' ]
};

The following examples will feature a hard coded object called event that mimics the dispatched DOM event to show the values being pressed and held.

All methods return undefined when there is no match:

import KeysKey from "keyskey";

const event = { key: 'A', metaKey: false, shiftKey: false, ctrlKey: false };
const result = KeysKey.Is(event, "a");
console.log(result); // // undefined

Can give an array of strings instead of KeysKey constants:

import KeysKey from "keyskey";

const event = { key: 'A', metaKey: false, shiftKey: false, ctrlKey: false };  
const result = KeysKey.Or(event, ["A", "a"]);
console.log(result); //  [ 'A' ]

Also works as a variadic function:

import KeysKey from "keyskey";

const event = { key: '1', metaKey: false, shiftKey: true, ctrlKey: false };
const result = KeysKey.And(event, KeysKey.Number.One, KeysKey.Modifier.Shift);
console.log(result) // [ '1', "Shift" ]

Example of a special key combo:

import KeysKey from "keyskey";

const event = { key: 'A', metaKey: false, shiftKey: true, ctrlKey: true };
const result = KeysKey.SpecialCombos.isControlAndShift(event);
console.log(result); // [ 'Control', 'Shift' ]

Another example of a special key combo:

import KeysKey from "keyskey";

const event = { key: '8', metaKey: false, shiftKey: false, ctrlKey: false };
const result = KeysKey.SpecialCombos.isDigit(event);
console.log(result); // [ '8' ]

Full Documentation

Generated Docs from JS Docstrings contain all the types.

Library Methods

Function NameParametersExplanation
KeysKey.Isevent, keysChecks if keys match the provided event.
KeysKey.Andevent, keysTakes a single key string, array of keys or operates as a variadic function.
KeysKey.Orevent, keysTrue if at least one of the keys match the event.

Note on .And, .is, and .Or methods:

These functions can take a single key string, an array of keys, or operate as a variadic function. The event parameter typically represents a keyboard event. The keys parameter can be a single key string, an array of key strings, or multiple separate key string arguments. The functions assess the keys against the event and return a boolean value based on the specific matching criteria (exact match, all keys match, or at least one key matches).

Special Combos

These can be targetted through: KeysKey.SpecialCombos.:

Function NameParametersExplanation
isAltAndControlevent: KeyEventReturns ["Alt", "Control"] if both Alt and Control keys are pressed, otherwise undefined.
isAltAndShiftevent: KeyEventReturns ["Alt", "Shift"] if both Alt and Shift keys are pressed, otherwise undefined.
isAltKeyevent: KeyEventReturns ["Alt"] if Alt key is pressed, otherwise undefined.
isAltOrShiftevent: KeyEventReturns ["Alt", "Shift"] if either Alt or Shift key is pressed, otherwise undefined.
isControlAndShiftevent: KeyEventReturns ["Control", "Shift"] if both Control and Shift keys are pressed, otherwise undefined.
isControlOrShiftevent: KeyEventReturns ["Control", "Shift"] if either Control or Shift key is pressed, otherwise undefined.
isDeleteevent: KeyEventReturns [event.key] if the Delete key is pressed (key code 46), otherwise undefined.
isDigitevent: KeyEventReturns [event.key] if a digit (0-9) is pressed, otherwise undefined.
isFunctionKeyevent: KeyEventReturns [event.key] if a function key (F1-F12) is pressed, otherwise undefined.
isEnglishLetterevent: KeyEventReturns [event.key] if a letter (A-Z, a-z) is pressed, otherwise undefined.
isLowercaseLetterevent: KeyEventReturns [event.key] if a lowercase letter (a-z) is pressed, otherwise undefined.
isMediaControlevent: KeyEventReturns [event.key] if a media control key is pressed, otherwise undefined.
isMetaAndControlevent: KeyEventReturns ["Meta", "Control"] if both Meta and Control keys are pressed, otherwise undefined.
isMetaAndShiftevent: KeyEventReturns ["Meta", "Shift"] if both Meta and Shift keys are pressed, otherwise undefined.
isMetaOrControlevent: KeyEventReturns ["Meta", "Control"] if either Meta or Control key is pressed, otherwise undefined.
isMetaOrShiftevent: KeyEventReturns ["Meta", "Shift"] if either Meta or Shift key is pressed, otherwise undefined.
isModifierevent: KeyEventReturns [event.key] if any modifier key (Meta, Shift, Control, Alt) is pressed, otherwise undefined.
isNavigationKeyevent: KeyEventReturns [event.key] if a navigation key is pressed, otherwise undefined.
isNonEnglishLetterevent: KeyEventReturns [event.key] if a non-English letter is pressed, otherwise undefined.
isSpecialCharacterevent: KeyEventReturns [event.key] if a special character is pressed, otherwise undefined.
isUppercaseLetterevent: KeyEventReturns [event.key] if an uppercase letter (A-Z) is pressed, otherwise undefined.
isWhitespaceevent: KeyEventReturns [event.key] if a whitespace key (Space, Tab) is pressed, otherwise undefined.

Types

TypeExplanation
NumberNumbers
LetterAlphabetic characters
SpecialCharacterNon-alphanumeric characters (e.g., !, @, #, $)
WhiteSpaceSpaces and line control characters (Enter, Space, Tab)
MultimediaKeys for media control (Play, Pause, Volume)
LockToggle keys (CapsLock, NumLock, ScrollLock)
FunctionFunction keys (F1, F2, ..., F12)
NavigationArrow keys, Page Up/Down, Home, End
EditingInsert, Delete, Backspace, etc.
ModifierShift, Ctrl, Alt, Windows/Command keys
SpecialKeysGroupCombination of all above keys except Number and Letter
AllKeysIncludes all key types above

Issues

Feel free to create an issue.

0.0.71

5 months ago

0.0.7

5 months ago

0.0.5

5 months ago

0.0.6

5 months ago

0.0.4

5 months ago

0.0.3

5 months ago

0.0.2

5 months ago

0.0.1

1 year ago