Create a gist now

Instantly share code, notes, and snippets.

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