modifier-keys

A declarative library designed to handle modifier key states

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
modifier-keys
1.2.16 years ago7 years agoMinified + gzip package size for modifier-keys in KB

Readme

Modifier Keys · npm version Build Status Coverage Status
A declarative library designed to handle modifier key states across multiple systems

Introduction

Instead of checking whether Command is pressed on macOS or control on Windows, using Modifier Keys you check for primaryKey and secondaryKey
  • Primary Key - set to be Command or Control depending on the operating system
  • Secondary Key - set to be Alt

Can also parse key commands to string according to the environment (e.g. Ctrl+C)

Installation

Using Yarn:
yarn add modifier-keys

Using npm:
npm install modifier-keys --save

Importing:
import Modifier from 'modifier-keys';

Usage

Closure Usage

The Modifier closure takes a function that takes an event handler as its first argument, it then adds the primary and secondary key states onto it.
import Modifier from 'modifier-keys';

<input type="text" onKeyDown={Modifier(this.handleKeyDown)} />

function handleKeyDown(e) {
  e.primaryKey; // bool
  e.secondaryKey; //bool
}

Function Usage

You can also import a function that directly takes the event instead of using a closure on the handler like so
import { modifier } from 'modifier-keys';

<input type="text" onKeyDown={this.handleKeyDown} />

function handleKeyDown(e) {
  let event = modifier(e);
  event.primaryKey; // bool
  event.secondaryKey; //bool
}

Command Parser Usage

Takes a key (string that will be capitalized) and options
import { parse } from 'modifier-keys';

parse('c', { primaryKey: true }); // ⌘C or Ctrl+C

Options

  • primaryKey - bool, to include or not the environment's primary key
  • secondaryKey - bool, to include or not the environment's secondary key