We can detect which arrow key ( left , right , up or down ) is pressed by using onkeydown event.
Press any arrow key.
Press any arrow key ( Demo )
Here is the source code
<script language='JavaScript' type='text/JavaScript'>
<!--
function disp(str){
//alert(str);
document.getElementById('my_msg').innerHTML=str;
}
document.onkeydown = function() {
switch (window.event.keyCode) {
case 37:
disp('Left key is pressed') // execute a function by passing parameter
break;
case 38:
disp('Up key is pressed')
break;
case 39:
disp('Right key is pressed')
break;
case 40:
disp('Down key is pressed')
break;
}
};
//-->
</script>