Keycodes in different browsers? JQuery

All I found about this is that there are inconsistencies between different web browsers when it comes to key codes.

I have the following code that works fine when I press enter in Safari, Opera and Chrome, but not in Firefox.

I am using FF 9.0.1

Here are two pieces of code where the problem occurs:

1)

//Post something
$('#postDiv').on('keydown', '#posttext', function(e) {
    if ((e.keyCode == 13 || e.which == 13) && !event.shiftKey) {
        post($(this));
    }
});

2)

//Comment on a post
$('.commenttext').live('keydown', function(e) {
    if ((e.keyCode == 13 || e.which == 13) && !event.shiftKey) {
        comment($(this));
    }
});

Both functions are not even called.

Thanks in advance :-) Dennis

EDIT 1

I did some more tests only with e.which and noticed that it works when I don't leave & &! event.shiftKey - apparently FF does not know this command. Is there an alternative to this? I would like the user to be able to press shift + enter without sending a message. I already tried the if condition (e.which == 13 & e.which! = 16), but to no avail.

EDIT 2 - SOLUTION

, ! , e.shiftKey, event.shiftKey! (, , IE).

+3
2

http://api.jquery.com/event.which/

event.which event.keyCode event.charCode. . .

e.keyCode, , e.which!= 13, e.keyCode. , e.keyCode ?

+4

keyCode, which, which, :)

http://api.jquery.com/event.which/

+1
source

All Articles