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>

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer