Created
March 7, 2016 11:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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