Skip to content

Instantly share code, notes, and snippets.

@lewdev
Last active June 27, 2023 21:10
Show Gist options
  • Select an option

  • Save lewdev/b3524f1669d6108ef485d2dafa5b5eea to your computer and use it in GitHub Desktop.

Select an option

Save lewdev/b3524f1669d6108ef485d2dafa5b5eea to your computer and use it in GitHub Desktop.
⌨️ Do the Konami Code!
<h1>⌨️ Do the Konami Code!</h1>
<script>
// https://jamiebuilds.github.io/tinykeys/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e||self).tinykeys={})}(this,function(e){var t=["Shift","Meta","Alt","Control"],n="object"==typeof navigator?navigator.platform:"",i=/Mac|iPod|iPhone|iPad/.test(n),o=i?"Meta":"Control",r="Win32"===n?["Control","Alt"]:i?["Alt"]:[];function a(e,t){return"function"==typeof e.getModifierState&&(e.getModifierState(t)||r.includes(t)&&e.getModifierState("AltGraph"))}function f(e){return e.trim().split(" ").map(function(e){var t=e.split(/\b\+/),n=t.pop();return[t=t.map(function(e){return"$mod"===e?o:e}),n]})}function u(e,n){var i;void 0===n&&(n={});var o=null!=(i=n.timeout)?i:1e3,r=Object.keys(e).map(function(t){return[f(t),e[t]]}),u=new Map,d=null;return function(e){e instanceof KeyboardEvent&&(r.forEach(function(n){var i=n[0],o=n[1],r=u.get(i)||i;!function(e,n){return!(n[1].toUpperCase()!==e.key.toUpperCase()&&n[1]!==e.code||n[0].find(function(t){return!a(e,t)})||t.find(function(t){return!n[0].includes(t)&&n[1]!==t&&a(e,t)}))}(e,r[0])?a(e,e.key)||u.delete(i):r.length>1?u.set(i,r.slice(1)):(u.delete(i),o(e))}),d&&clearTimeout(d),d=setTimeout(u.clear.bind(u),o))}}e.createKeybindingsHandler=u,e.parseKeybinding=f,tinykeys=e.tinykeys=function(e,t,n){var i;void 0===n&&(n={});var o=null!=(i=n.event)?i:"keydown",r=u(t,n);return e.addEventListener(o,r),function(){e.removeEventListener(o,r)}}});
tinykeys(window, {
"ArrowUp ArrowUp ArrowDown ArrowDown ArrowLeft ArrowRight ArrowLeft ArrowRight b a": () => {
alert("KONAMI CODE");
},
//Other examples
"Shift+D": () => {
alert("The 'Shift' and 'd' keys were pressed at the same time")
},
"y e e t": () => {
alert("The keys 'y', 'e', 'e', and 't' were pressed in order")
},
"$mod+KeyD": event => {
event.preventDefault()
alert("Either 'Control+d' or 'Meta+d' were pressed")
},
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment