Skip to content

Instantly share code, notes, and snippets.

@lewdev
Last active August 23, 2023 03:25
Show Gist options
  • Select an option

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

Select an option

Save lewdev/5b118858e3018437591b9a4d299e8e62 to your computer and use it in GitHub Desktop.
⌨ Minified JS Keyboard Inputs (WASD, ZQSD, & Arrow Keys)

⌨ Minified JS Keyboard Inputs (WASD, ZQSD, & Arrow Keys)

Source: JS Keyboard Inputs

Code to demonstrate capability (108b)

I reduced the original size from 115b by 7 characters by using repeat.

This is a clever solution where it's finding u, d, l, r in the string using e.which which is the keyboard code and finding the index of it minus 37.

This is very cool to have in support small games in bookmarklets or other types of games with very little code.

Supporting arrow keys actually increases the size of the code significantly. We could reduce it by 21 characters if we don't support arrow keys.

Supports WASD, ZQSD, & Arrow keys (108b)

<p id=o></p>
<script>
u=r=d=l=0;onkeydown=onkeyup=e=>this[`lurd${"*".repeat(24)}l**r************l*d***u**u`[e.which-37]]=e.type[5]

setInterval(() => o.innerHTML = JSON.stringify({u,r,d,l}), 30);
</script>

Supports WASD & ZQSD (87b)

u=r=d=l=0
onkeydown=onkeyup=e=>this['l**r************l*d***u**u'[e.which-65]]=e.type[5]

Supports WASD Only (84b)

Removing support for ZQSD only saves 3 characters.

u=r=d=l=0
onkeydown=onkeyup=e=>this['l**r**************d***u'[e.which-65]]=e.type[5]

Move a ball (303b)

data:text/html,<meta charset=utf8><body style=margin:0;overflow:hidden onload="x=c.getContext`2d`;u=r=d=l=X=Y=0;onkeydown=onkeyup=e=>this['l**r************l*d***u**u'[e.which-65]]=e.type[5];setInterval`c.width=99;x.fillText('⚽',X+=l?-1:r?1:0,Y+=u?-1:d?1:0)`"><canvas id=c style=width:100vw;height:100vw>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment