OnKeyDown to get arrow key pressed

We can detect which arrow key ( left , right , up or down ) is pressed by using onkeydown event. 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>

HTML

<div id=my_msg></div>
Visitors Rating
Your Rating




Google+

Post Comment This is for short comments only. Use the forum for more discussions.




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer