A declarative library designed to handle modifier key states across multiple systems
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
)
Using Yarn:
yarn add modifier-keys
Using npm:
npm install modifier-keys --save
Importing:
import Modifier from 'modifier-keys';
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
}
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
}
Takes a key
(string that will be capitalized) and options
import { parse } from 'modifier-keys';
parse('c', { primaryKey: true }); // ⌘C or Ctrl+C
primaryKey
- bool, to include or not the environment's primary keysecondaryKey
- bool, to include or not the environment's secondary key