I need to see if the keys: A, S, or D were pressed

Discussion in 'JavaScript' started by bobby9101, May 8, 2007.

  1. #1
    Hi, I have a page and I need the page to pop-up a little alert box when the keys: A, S, or D are pressed
    How do I do this?
    Thanks!
     
    bobby9101, May 8, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <html>
    <head>
    <script type="text/javascript">
    <!--
    window.onkeydown = function(e) {
        if (!e)
            var e = window.event;
        if (e.keyCode)
            var code = e.keyCode;
        else if (e.which)
            var code = e.which;
        switch(String.fromCharCode(code)) {
            case 'A':
                alert('A was pressed');
                break;
            case 'S':
                alert('S was pressed');
                break;
            case 'D':
                alert('D was pressed');
                break;
        }
    }
    -->
    </script>
    </head>
    <body>
    My document
    </body>
    </html>
    HTML:
     
    rodney88, May 9, 2007 IP