Skip to content

Instantly share code, notes, and snippets.

@tamalw
Created September 14, 2011 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamalw/1215877 to your computer and use it in GitHub Desktop.
Save tamalw/1215877 to your computer and use it in GitHub Desktop.
JavaScript key bindings
<html>
<head>
<title>JS Key Example</title>
<script type="text/javascript">
addEventListener("keydown", function (e) {
var output = document.getElementById('output');
if (e.keyCode == 37) {
output.innerHTML = "You pressed left."
}
if (e.keyCode == 38) {
output.innerHTML = "You pressed up."
}
if (e.keyCode == 39) {
output.innerHTML = "You pressed right."
}
if (e.keyCode == 40) {
output.innerHTML = "You pressed down."
}
}, false);
</script>
</head>
<body>
<h1 id="output"></h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment