Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to detect the arrow key on press using Javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 531
    Comment on it

    Hello reader's! If you are making the website based on user press key events. Then to detect the arrow key pressed by user we can code as below:-

    document.onkeydown = function(e) {
        switch (e.keyCode) {
            case 37:
                alert('You just press Left key');//Left arrow key is pressed
                break;
            case 38:
                alert('You just press Up key');//Up arrow key is pressed
                break;
            case 39:
                alert('You just press Right key'); //right arrow key is pressed
                break;
            case 40:
                alert('You just press Down key'); //Down arrow key is pressed
                break;
        }
    };
    

    The output will be an alert box saying 'Left arrow key is pressed'

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: