Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q402 Is there a way to invoke a function when a certain button (specifically the left or right arrow key) is pressed?

irt.org | Knowledge Base | JavaScript | Misc | Q402 [ previous next ]

Q402 Is there a way to invoke a function when a certain button (specifically the left or right arrow key) is pressed?

It isn't possible to capture the cursor keys themselves. However, the following will capture the < and > keys in Netscape Navigator 4 and/or Internet Explorer 4:

<script language="JavaScript1.2"><!--
function netscapeKeyPress(e) {
    if (e.modifiers & Event.SHIFT_MASK) {
        if (e.which == 60) alert('< pressed');
        if (e.which == 62) alert('> pressed');
    }

}

function microsoftKeyPress() {
    if (window.event.shiftKey) {
         if (window.event.keyCode == 60) alert('< pressed');
         if (window.event.keyCode == 62) alert('> pressed');
    }
}

if (navigator.appName == 'Netscape') {
    window.captureEvents(Event.KEYPRESS);
    window.onKeyPress = netscapeKeyPress;
}
//--></script>

</head>

<body onKeyPress="microsoftKeyPress()">


</body>

Feedback on 'Q402 Is there a way to invoke a function when a certain button (specifically the left or right arrow key) is pressed?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.