www.webdeveloper.com
Results 1 to 6 of 6

Thread: Capturing Key Strokes

  1. #1
    Join Date
    Aug 2008
    Posts
    47

    Capturing Key Strokes

    I have the follow code:

    Code:
     var captureKeys = function(ev) {
         ev = ev || window.event;             // gets the event in ie or ns
         kCode = ev.keyCode || ev.which;   // gets the keycode in ie or ns
      
         /* in ie, when pressing the ctrl + shift + key, it gives the key code for the capitalized key (probably because shift is pressed) 
            in ns pressing ctrl, shift and another key doesn't change the keycode
            thus, the || and two different numbers */
      
         if (ev.ctrlKey && ev.shiftKey && kCode == 19 || ev.ctrlKey && ev.shiftKey && kCode == 83) {    // ctrl+shift+s
    		Over(1);
             return false;  // make it so the browser ignores key combo
         }
         if (ev.ctrlKey && ev.shiftKey && kCode == 90 || ev.ctrlKey && ev.shiftKey && kCode == 90) {    // ctrl+shift+z
    		Over(1);
             return false;  // make it so the browser ignores key combo
         }
    
    }
    This code is supposed to capture CTRL+Shift+Key, and when certain combos are pressed, it should activate a new prompt on screen. For some reason the CTRL+Shift+S works but I can not understand how to get it to work with any other Key Codes. Could someone please help me with this and show me what I'm doing wrong? Thanks In Advance. -Gary

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,257

    Lightbulb

    What are the other keycodes you want?
    See: http://www.cambiaresearch.com/c4/702...Key-Codes.aspx

  3. #3
    Join Date
    Aug 2008
    Posts
    47
    I actually have about 11 different sets using CTRL+Shift + the key. Like I just changed 90(Z) to 79(O) and 111 to see if either of those would work but that only supports FF as IE refused to open the window. I would like to be able to use A,H,S,Q,N,M,R,T,P,E, and D with this to get it to prompt for an override on an Application I am building to make exceptions. I'm not sure how to get it to work properly between IE and FF to be honest.

  4. #4
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Just a little something I was playing around with regarding this, I'm actually planning on doing a bit more key code work myself -possibly making a sort of 'key code manager' which will make it a snap for people to associate keyCode's with actions. At any rate, I was playing with your example and incorporating somewhat what you need, I'm guessing your biggest problem right now (since we can't see how you are invoking the captureKeys function, or what's happening in the "Over" function -although I'm assuming from what you say it triggers prompt's or window's) is probably related to needing a bit of a delay before invoking the "Over" function if it triggers any sort of prompt's, alert's or window's, see note in my play case below, which hopefully you may be able to figure out how to manipulate it to your needs -if not, let us know:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title>astupidtitle</title>
    <script type="text/javascript">
    function captureKeys(ev) {
        var ev = ev || window.event;             // gets the event in ie or ns
        var kCode = ev.keyCode || ev.which;   // gets the keycode in ie or ns
        var codeActions = {
    /***
    just something I was playing around with,
    in the values (which must always be a string, here) a colon denotes the end of a function name and 
    beginning of an argument list to send to the function, the argument list
    uses commas to seperate it's values in to an array
    ***/
            "65":"Over:1", //a
            "68":"Over:2", //d
            "69":"Over:3", //e
            "72":"Over:4", //h
            "77":"Over:5", //m
            "78":"Over", //n
            "80":"Over", //p
            "81":"out", //q
            "82":"out:This is an argument", //r
            "83":"Over:10,hello world", //s
            "84":"Over:11,argument 2,argument3" //t
        };
        if (ev.ctrlKey && ev.shiftKey && kCode in codeActions) {
            var actions = codeActions[kCode];
            actions = actions.split(':');
            var funcName = actions[0];
            var args = (actions[1]) ? actions[1].split(',') : null;
            if (typeof window[funcName] == 'function') {
    /***a slight delay for this function to finish and exit first (returning false) before 
    the next function is triggered is needed here, else if the function being triggered has 
    any alert's or prompt's they cause a substantial delay and the browser's default action 
    associated with the key press may occur (if the setTimeout were not used here, in other words)
    ***/
                window.setTimeout(function () { window[funcName](args); }, 1);
                return false;
            }
        }
        return true;
    };
    function Over(args) {
        var i, s = 'args is not an array, you must have pressed n or p';
        if (args instanceof Array) {
            for (i = 0, s = ''; i < args.length; i++) {
                s += '('+ typeof args[i] +') '+ args[i] +'<br>';
            }
        }
        document.getElementById('output').innerHTML = s;
        alert(s.replace(/<br>/g,'\n'));
    }
    function out(s) {
        alert(s || 'no argument');
    }
    document.onkeydown = function (event) { return captureKeys(event); };
    </script>
    </head>
    <body>
    <p style="text-align:center;">Pressing ctrl+shift+ (a, d, e, h, m, n, p, q, r, s, or t) will trigger actions</p>
    <p id="output" style="width:auto; text-align:center; margin:20px auto; border:1px solid red; padding:4px;"></p>
    </body>
    </html>

  5. #5
    Join Date
    Aug 2008
    Posts
    47
    Thank you. This worked out excellent. I've modified the code a hair but it works perfectly and the best part is it's easily modified for additional keys being needed. Thanks again. -Gary

  6. #6
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    You're welcome, glad it helped!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles