Skip to content

Instantly share code, notes, and snippets.

@Sominemo
Last active May 31, 2018 20:31
Show Gist options
  • Save Sominemo/e3c7a24bb3968c950ef6e919ad4b0406 to your computer and use it in GitHub Desktop.
Save Sominemo/e3c7a24bb3968c950ef6e919ad4b0406 to your computer and use it in GitHub Desktop.
Listening for pressing keys from string in correct order and giving callback
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!");})
@Sominemo
Copy link
Author

@ubuntuegor
Copy link

Very nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment