Skip to content

playbanq/keyboardEvents

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

keyboardEvents

Simple keydown, keyup and keyhold event handling.

Usage

First, request the script in an HTML document:

    <script src='path/to/keyboardEvents.js'></script>

Then, create an event emitter by calling the #emitter method of the KeyboardEvents global variable:

var keyboard = new KeyboardEvents.emitter();

Finally, subscribe to keyboard events using the #on method:

// (custom_key_name, keyCode, callbacksObject)
keyboard.on('up_arrow', 39, {
  'onkeydown': function () {
    // Do something when the key is pressed
  },
  'onkeyhold': function (delta) {
    // Do something every 'delta' miliseconds the key is hold down
    // The default interval is 33 milliseconds, equivalent to 30 fps
  },
  'onkeyup': function () {
    // Do something when the key is released
  }
});

Subscriptions to key events can be cancelled all at once or separately using the #off method:

// Unsubscribe from all the events of the up arrow key
    keyboard.off('up_arrow', 39);
// Unsubscribe from one event by providing a string with the event name
    keyboard.off('up_arrow', 39, 'onkey____');
// Unsubscribe from more than one event by providing a space separated string of event names
    keyboard.off('up_arrow', 39, 'onkey____ onkey____');

Options

The duration of the interval between 'onkeyhold' events can be set by passing an options object on calls to the #emitter method:

// If keys are hold down, emit keyhold events every 100 milliseconds instead of the default 33:
var keyboard = KeyboardEvents.emitter({
    keyholdInterval: 100
});

About

Keydown, keyup and keyhold event handling

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published