Bug Tracker

Opened 9 years ago

Closed 9 years ago

Last modified 6 years ago

#7183 closed bug (cantfix)

Keypress e.which, UP, DOWN, LEFT and RIGHT

Reported by: da__smith@… Owned by:
Priority: low Milestone:
Component: event Version: 1.4.3
Keywords: Cc:
Blocked by: Blocking:

Description

Hi jQuery

Test case is the example on: http://api.jquery.com/keypress/

Keypress DOWN, UP, LEFT and RIGHT all return 0 for e.which. (there may be other keys like PAGEUP and PAGEDOWN, apologies for not being thorough)

Although the test case is on 1.4.2. The bug is still there in 1.4.3rc2.

The browser used was Firefox 3.6.10 on OSX 10.5.8.

all the best and thanks for jQuery Dave Smith, da__smith@…

Change History (10)

comment:1 Changed 9 years ago by addyosmani

Component: unfiledevent
need: ReviewPatch
Priority: undecidedlow
Status: newopen

You could try using something like this

var keycode = (event.keyCode ? event.keyCode : event.which);

This bug may be related to: #6489, #3311. I know it was mentioned we would be targeting a fix for this in 1.4.3 but as it still appears to be an issue (I've tested and experienced the above in FF 3.6.10/OSX as well), I'll flag for a further review and patch.

comment:2 Changed 9 years ago by da__smith@…

Many thanks addyosmani, the suggestion worked, I went with the following in the end:

e.which = e.which | | e.keyCode;

comment:3 Changed 9 years ago by snover

Milestone: 1.4.3

Resetting milestone to future.

comment:4 Changed 9 years ago by da__smith@…

Hi jQuery

Just tried out 1.5 RC1 but unfortunately the bug remains. Thought I'd share here with a test page for fun: http://www.dave-smith.info/jquery-1.5-rc1-keys-test/

all the best Dave, da__smith@…

comment:5 Changed 9 years ago by dmethvin

Another test case from dup #4556: http://jsfiddle.net/boushley/YxunV/1/

comment:6 Changed 9 years ago by dmethvin

#4556 is a duplicate of this ticket.

comment:7 Changed 9 years ago by tomgrohl

I've done some tests and keypress doesn't even get called in Chrome, Safari and IE for the arrow keys etc, but does in Firefox and Opera.

For Opera its the other way round, for the letters only the keydown events run, and both events run for the arrows keys etc.

This is probably why keydown and keyup generally used and not keypress.

I came up with the following, which both work:

// Add which for key events
if ( event.which == null && (event.charCode != null || event.keyCode != null) ) {
    event.which = event.charCode != null ? event.charCode : event.keyCode;
} else if ( event.which === 0 && event.keyCode != null ) {
    //On keypress events 'which' is not set, but is on keydown and keyup
    //So here we normalise it
    event.which = event.keyCode;
}

OR

if ( (event.which == null || event.which === 0 ) && (event.charCode != null || event.keyCode != null)) {
    event.which = ( event.charCode != null && event.charCode !== 0 ) ? event.charCode : event.keyCode;
}

So something like this might be good to use. Second one may be better as its less code. It just makes 'which' the same value as 'keyCode' if 'which' is 0 and 'keyCode' is set (not null).

'which' is always set to 0 when the arrows keys are pressed during the keypress event, so one of these ways seems right.

Needs to be tested thoroughly though but looks promising.

comment:8 Changed 9 years ago by john

Resolution: cantfix
Status: openclosed

Yeah, you're not supposed to be using keypress for these values - you should be using keyup or keydown instead. The results are not consistent across browsers and there isn't a whole lot that we can do to resolve it.

comment:9 in reply to:  8 Changed 9 years ago by tomgrohl

Replying to john:

Yeah, you're not supposed to be using keypress for these values - you should be using keyup or keydown instead. The results are not consistent across browsers and there isn't a whole lot that we can do to resolve it.

Yeah I read that when I looked it up. Not really a bug in jQuery then. Thought I'd have a go a fixing it anyway. Doesn't matter now. :)

comment:10 Changed 6 years ago by dmethvin

#14669 is a duplicate of this ticket.

Note: See TracTickets for help on using tickets.