Last active
May 31, 2018 20:31
-
-
Save Sominemo/e3c7a24bb3968c950ef6e919ad4b0406 to your computer and use it in GitHub Desktop.
Listening for pressing keys from string in correct order and giving callback
This file contains 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
onword={reg:{},set:(d,e,f)=>{f||(d=d.toLowerCase()),document.addEventListener("keypress",g=>{onword.listen(d,g,e,f)}),onword.reg[d]=0},listen:(d,e,f,g)=>{if(!g)var h=e.key.toLowerCase();else var h=e.key;if(d[onword.reg[d]]&&h===d[onword.reg[d]])onword.reg[d]++;else{onword.reg[d];onword.reg[d]=0}onword.reg[d]>=d.length&&(f(),onword.reg[d]=0)}}; | |
// onword.set(): Add new KeyPress-listener | |
// @param d* {string} - String we're listening for | |
// @param e* {function} - Callback | |
// @param f {bool} - Case-sensitive | |
onword.set("Hello World", ()=>{alert("Welcome to JavaScript!");}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice