<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<meta charset="utf-8">
<style>
#canvas {
border: solid black 1px;
}
</style>
</head>
<body>
<div></div>
<canvas id="canvas" height="100" width="100"></canvas>
<script>
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
document.body.addEventListener('keydown', fn,false);
var x = 10, y = 10;
function fn (e) {
ctx.clearRect(0,0, 100,100);
if(e.keyCode == 37) x-=1;
if(e.keyCode == 38) y-=1;
if(e.keyCode == 39) x+=1;
if(e.keyCode == 40) y+=1;
ctx.fillRect(x,y, 10, 10);
}
</script>
</body>
</html>