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

Q259 Is is possible to trap Shift and/or Ctrl key sequences?

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

In Netscape Navigator 4 and Internet Explorer 4 its possible to trap key presses. The following *little* example demonstrates Shift or Ctrl key combinations. Most of the Ctrl keys are already reserved. The Shift keys maybe no use as it traps people entering upper case characters:

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript1.2"><!--
function netscapeKeyPress(e) {
    if (e.modifiers == 2) {
        // a - Select all
        // b - Edit bookmarks
        // c - Copy
        // d - Add bookmark
        if (e.which == 5 ) alert('Ctrl and e pressed');
        // f - Find in page
        // g - Find Again
        // h - Open History
        // i - Page Info
        if (e.which == 10) alert('Ctrl and j pressed');
        if (e.which == 11) alert('Ctrl and k pressed');
        if (e.which == 12) alert('Ctrl and l pressed');
        // m - New Message
        // n - New Navigator Window
        // o - Open Page
        // p - Print
        // q - Exit
        // r - Reload
        // s - Save As
        if (e.which == 20) alert('Ctrl and t pressed');
        // u - Page source
        // v - Paste
        // w - Close
        // x - Cut
        if (e.which == 25) alert('Ctrl and y pressed');
        // ?
    }
    else if (e.modifiers == 4) {
        if (e.which == 65) alert('Shift and A pressed');
        if (e.which == 66) alert('Shift and B pressed');
        if (e.which == 67) alert('Shift and C pressed');
        if (e.which == 68) alert('Shift and D pressed');
        if (e.which == 69) alert('Shift and E pressed');
        if (e.which == 70) alert('Shift and F pressed');
        if (e.which == 71) alert('Shift and G pressed');
        if (e.which == 72) alert('Shift and H pressed');
        if (e.which == 73) alert('Shift and I pressed');
        if (e.which == 74) alert('Shift and J pressed');
        if (e.which == 75) alert('Shift and K pressed');
        if (e.which == 76) alert('Shift and L pressed');
        if (e.which == 77) alert('Shift and M pressed');
        if (e.which == 78) alert('Shift and N pressed');
        if (e.which == 79) alert('Shift and O pressed');
        if (e.which == 80) alert('Shift and P pressed');
        if (e.which == 81) alert('Shift and Q pressed');
        if (e.which == 82) alert('Shift and R pressed');
        if (e.which == 83) alert('Shift and S pressed');
        if (e.which == 84) alert('Shift and T pressed');
        if (e.which == 85) alert('Shift and U pressed');
        if (e.which == 86) alert('Shift and V pressed');
        if (e.which == 87) alert('Shift and W pressed');
        if (e.which == 88) alert('Shift and X pressed');
        if (e.which == 89) alert('Shift and Y pressed');
        if (e.which == 90) alert('Shift and Z pressed');
    }
}

function microsoftKeyPress() {
    if (window.event.ctrlKey) {
        // a - Select All
        // b -Organise Favourites
        // c - Copy
        // d -Add to Favourites
        if (window.event.keyCode == 5 ) alert('Ctrl and e pressed');
        // f -Find (on this page)
        if (window.event.keyCode == 7 ) alert('Ctrl and g pressed');
        if (window.event.keyCode == 8 ) alert('Ctrl and h pressed');
        if (window.event.keyCode == 9 ) alert('Ctrl and i pressed');
        if (window.event.keyCode == 10) alert('Ctrl and j pressed');
        if (window.event.keyCode == 11) alert('Ctrl and k pressed');
        // l - Open
        if (window.event.keyCode == 13) alert('Ctrl and m pressed');
        // n - New Window
        // o - Open
        // p - Print
        if (window.event.keyCode == 17) alert('Ctrl and q pressed');
        // r - Reload
        // s - Save
        if (window.event.keyCode == 20) alert('Ctrl and t pressed');
        if (window.event.keyCode == 21) alert('Ctrl and u pressed');
        // v - Paste
        // w - Close
        // x -Cut
        if (window.event.keyCode == 25) alert('Ctrl and y pressed');
        if (window.event.keyCode == 26) alert('Ctrl and z pressed');
    }
    else if (window.event.shiftKey) {
        if (window.event.keyCode == 65) alert('Shift and A pressed');
        if (window.event.keyCode == 66) alert('Shift and B pressed');
        if (window.event.keyCode == 67) alert('Shift and C pressed');
        if (window.event.keyCode == 68) alert('Shift and D pressed');
        if (window.event.keyCode == 69) alert('Shift and E pressed');
        if (window.event.keyCode == 70) alert('Shift and F pressed');
        if (window.event.keyCode == 71) alert('Shift and G pressed');
        if (window.event.keyCode == 72) alert('Shift and H pressed');
        if (window.event.keyCode == 73) alert('Shift and I pressed');
        if (window.event.keyCode == 74) alert('Shift and J pressed');
        if (window.event.keyCode == 75) alert('Shift and K pressed');
        if (window.event.keyCode == 76) alert('Shift and L pressed');
        if (window.event.keyCode == 77) alert('Shift and M pressed');
        if (window.event.keyCode == 78) alert('Shift and N pressed');
        if (window.event.keyCode == 79) alert('Shift and O pressed');
        if (window.event.keyCode == 80) alert('Shift and P pressed');
        if (window.event.keyCode == 81) alert('Shift and Q pressed');
        if (window.event.keyCode == 82) alert('Shift and R pressed');
        if (window.event.keyCode == 83) alert('Shift and S pressed');
        if (window.event.keyCode == 84) alert('Shift and T pressed');
        if (window.event.keyCode == 85) alert('Shift and U pressed');
        if (window.event.keyCode == 86) alert('Shift and V pressed');
        if (window.event.keyCode == 87) alert('Shift and W pressed');
        if (window.event.keyCode == 88) alert('Shift and X pressed');
        if (window.event.keyCode == 89) alert('Shift and Y pressed');
        if (window.event.keyCode == 90) alert('Shift and Z pressed');
    }
}

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

</HEAD>

<BODY onKeyPress="microsoftKeyPress()">

</BODY>
</HTML>

Feedback on 'Q259 Is is possible to trap Shift and/or Ctrl key sequences?'

©2018 Martin Webb