views:

120

answers:

1

This javascript code always returns zero for " Shift + / " key combination on Firefox 3.6.3 on OSX 10.5.8 But it returns the expected value 191 on Chrome on OSX/mac

GetKeyCode = function(e) {
        var code = 0;
        if (!e) {
            e = window.event
        }
        if (e.keyCode) { 
            code = e.keyCode;
        } else if (e.which) { 
            code = e.which;
        }
        return code;
    };

GetKeyCode is getting keydown event from jQuery.

jQuery(document).keydown(function(e) { ...... });

Is there any bug, or am I missing something very simple here? Please help

Thanks in advance. -Parimal Das

A: 

If it's coming from jQuery, you should be able to use e.which only. In fact, looking for e.keyCode may be causing the problem, since it's not populated if the Shift key is down. See the Mozilla documentation.

Mike McCaughan
I think you confuse `keyCode` with `charCode`: "`charCode` is never set in the keydown and keyup events. In these cases, `keyCode` is set instead." Also look at [event.keyCode](https://developer.mozilla.org/en/DOM/event.keyCode): "`keyCode` is always set in the keydown and keyup events. In these cases, `charCode` is never set."
Marcel Korpel
But you're right about jQuery normalizing `event.which`: "While browsers use differing properties to store this information, jQuery normalizes the `.which` property so we can reliably use it to retrieve the key code. This code corresponds to a key on the keyboard, including codes for special keys such as arrows. For catching actual text entry, `.keypress()` may be a better choice.
Marcel Korpel