Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

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

You are here: irt.org | FAQ | JavaScript | Misc | Q402 [ previous next ]

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?'

©2018 Martin Webb