Skip to content

Instantly share code, notes, and snippets.

@alessioalex
Created March 7, 2016 11:26
Show Gist options
  • Save alessioalex/033ce582e34625472e62 to your computer and use it in GitHub Desktop.
Save alessioalex/033ce582e34625472e62 to your computer and use it in GitHub Desktop.
Related to https://twitter.com/timferro/status/705189339890843648 - notify when the user types in a certain word / sequence of chars
function shortcut(shortcutKeys, callback) {
var code = shortcutKeys.toString();
var keys = [];
document.addEventListener('keydown', function(evt) {
keys.push(evt.keyCode);
if (keys.length > shortcutKeys.length) {
keys.shift();
}
if ((keys.length === shortcutKeys.length) && (keys.toString() === code)) {
callback();
}
});
}
// type 'alexandru'
shortcut([65, 76, 69, 88, 65, 78, 68, 82, 85], function() { console.log('shortcut detected'); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment