Description

This plugin adds a new event to jQuery. It works similar to the keydown, keyup and keypress events but instead of getting fired for each key it will only get fired if a specified key code combination was entered. Therefore it shares the behaviour with the other key events. This means that the element with the event must have the focus to capture the entered keys.

Initially this plugin was only designed to add the Konami Code to a web site. But after some cleaning it became a customizable event. Additionally it comes with a helper class called KeySequenceHelper to convert strings to key code arrays and the other way around.

Usage

.keysequence( sequence, handler(eventObject) )

  • sequence An array of key codes which trigger the event.
  • handler(eventObject) A function to execute each time the event is triggered.

.keysequence( sequence, [ eventData ], handler(eventObject) )

  • sequence An array of key codes which trigger the event.
  • eventData A map of data that will be passed to the event handler. (optional)
  • handler(eventObject) A function to execute each time the event is triggered.

.keysequence()

Examples

// event initialization with a key code array
var keyCodeArray = [65,67,68,69];
$(window).keysequence(keyCodeArray, function(event) {
	var string = KeySequenceHelper.keyCodeArrayToString(event.sequence);
	$('body').append(event.sequence)); // 65, 67, 68, 69
	$('body').append(string); // ABCD
});

// event initialization with a string
var keyCodeArray = KeySequenceHelper.stringToKeyCodeArray('foobar');
$(window).keysequence(keyCodeArray, function(event) {
	var string = KeySequenceHelper.keyCodeArrayToString(event.sequence);
	$('body').append(string); // FOOBAR
});

// event initialization with the Konami Code
$(window).keysequence(KeySequenceHelper.konamiCode, function(event) {
	$('body').append('Konami!');
});

// manual event triggering
$(window).keysequence();

Download