JavaScript KeyCode Values are “undefined” in Internet Explorer 8

前端 未结 8 1306
广开言路
广开言路 2020-11-30 12:58

I\'m having trouble with some JavaScript that I\'ve written, but only with Internet Explorer 8. I have no problem executing this on Internet Explorer 7 or earlier or on Mozi

相关标签:
8条回答
  • 2020-11-30 13:06

    I think window.Event.keyCode works in IE8 (I can't test right now though)

    0 讨论(0)
  • 2020-11-30 13:09

    I personally prefer the multi-key approach. This allows multiple keys to be detected, but also a single key just the same, and it works in every browser I've tested.

    map={}//declare object to hold data
    onkeydown=onkeyup=function(e){
        e=e||event//if e doesn't exist (like in IE), replace it with window.event
        map[e.keyCode]=e.type=='keydown'?true:false
        //Check for keycodes
    }
    

    An alternative method would be to separate the onkeydown and onkeyup events and explicitly define the map subitems in each event:

    map={}
    onkeydown=function(e){
        e=e||event
        map[e.keyCode]=true
    }
    onkeyup=function(e){
        e=e||event
        map[e.keyCode]=false
    }
    

    Either way works fine. Now, to actually detect keystrokes, the method, including bug fixes, is:

    //[in onkeydown or onkeyup function, after map[e.keyCode] has been decided...]
    if(map[keycode]){
        //do something
        map={}
        return false
    }
    

    map[keycode] constitutes a specific keycode, like 13 for Enter, or 17 for CTRL.

    The map={} line clears the map object to keep it from "holding" onto keys in cases of unfocusing, while return false prevents, for example, the Bookmarks dialog from popping up when you check for CTRL+D. In some cases, you might want to replace it with e.preventDefault(), but I've found return false to be more efficient in most cases. Just to get a clear perspective, try it with CTRL+D. Ctrl is 17, and D is 68. Notice that without the return false line, the Bookmarks dialog will pop up.

    Some examples follow:

    if(map[17]&&map[13]){//CTRL+ENTER
        alert('CTRL+ENTER was pressed')
        map={}
        return false
    }else if(map[13]){//ENTER
        alert('Enter was pressed')
        map={}
        return false
    }
    

    One thing to keep in mind is that smaller combinations should come last. Always put larger combinations first in the if..else chain, so you don't get an alert for both Enter and CTRL+ENTER at the same time.

    Now, a full example to "put it all together". Say you want to alert a message that contains instructions for logging in when the user presses SHIFT+? and log in when the user presses ENTER. This example is also cross-browser compatible, meaning it works in IE, too:

    map={}
    keydown=function(e){
        e=e||event
        map[e.keyCode]=true
        if(map[16]&&map[191]){//SHIFT+?
            alert('1) Type your username and password\n\n2) Hit Enter to log in')
            map={}
            return false
        }else if(map[13]){//Enter
            alert('Logging in...')
            map={}
            return false
        }
    }
    keyup=function(e){
        e=e||event
        map[e.keyCode]=false
    }
    onkeydown=keydown
    onkeyup=keyup//For Regular browsers
    try{//for IE
        document.attachEvent('onkeydown',keydown)
        document.attachEvent('onkeyup',keyup)
    }catch(e){
        //do nothing
    }
    

    Note that some special keys have different codes for different engines. But as I've tested, this works in every browser I currently have on my computer, including Maxthon 3, Google Chrome, Internet Explorer (9 and 8), and Firefox.

    I hope this was helpful.

    0 讨论(0)
  • 2020-11-30 13:11

    It looks like under IE8 the keyCode property of window.Event is undefined but that same property of window.event (note the lowercase e) has the value. You might try using window.event.

    function doSubmit(e)
    {
       var keyCode = (window.event) ? e.which : e.keyCode;
       if (keyCode == 13)
          document.getElementById("ctl00_ContentPlaceHolder1_Login").click();  
    }
    
    0 讨论(0)
  • 2020-11-30 13:13

    It's worked on this way on my code:

    var kcode = (window.event) ? event.keyCode : event.which;
    
    0 讨论(0)
  • 2020-11-30 13:15

    Just a hunch, try this:

    var keyCode = e.keyCode ? e.keyCode : e.which;
    
    0 讨论(0)
  • 2020-11-30 13:25

    Try adding onkeyup event as well and call the same function.

    TIP: You can add debugger; at beginning of doSubmit to set a break, then you can examine keyCode.

    0 讨论(0)
提交回复
热议问题