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

Q954 How can I determine which function key has been pressed?

You are here: irt.org | FAQ | JavaScript | Event | Q954 [ previous next ]

Function keys do not return key values.

Use the following to test this:

<html>
<head>
<script language="JavaScript1.2"><!--
function netscapeKeyPress(e) {
     if (e.modifiers & Event.CONTROL_MASK)
         document.test.control.value = 'true';
     else
         document.test.control.value = '';
     if (e.modifiers & Event.ALT_MASK)
         document.test.alt.value = 'true';
     else
         document.test.alt.value = '';
     if (e.modifiers & Event.SHIFT_MASK)
         document.test.shift.value = 'true';
     else
         document.test.shift.value = '';
     document.test.key.value = e.which;
}

function microsoftKeyPress() {
    var prefix = '';
    if (window.event.ctrlKey)
         document.test.control.value = 'true';
     else
         document.test.control.value = '';
    if (window.event.altKey)
         document.test.alt.value = 'true';
     else
         document.test.alt.value = '';
    if (window.event.shiftKey)
         document.test.shift.value = 'true';
     else
         document.test.shift.value = '';
    document.test.key.value = window.event.keyCode;
}

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

</head>

<body onKeyPress="microsoftKeyPress()">

<form name="test">
<p>Test<br><input type="text">

<p>Key<br><input type="text" name="key">
<p>Control<br><input type="text" name="control">
<p>Alt<br><input type="text" name="alt">
<p>Shift<br><input type="text" name="shift">

</form>

</body>
</html>

Feedback on 'Q954 How can I determine which function key has been pressed?'

©2018 Martin Webb