Skip to main content

Bug Tracker

Side navigation

#7183 closed bug (cantfix)

Opened October 13, 2010 10:06PM UTC

Closed April 17, 2011 05:37PM UTC

Last modified January 04, 2014 08:04PM UTC

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

Reported by: da__smith@hotmail.com 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@hotmail.com

Attachments (0)
Change History (10)

Changed October 13, 2010 11:36PM UTC by addyosmani comment:1

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.

Changed October 15, 2010 07:20PM UTC by da__smith@hotmail.com comment:2

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

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

Changed November 12, 2010 02:40AM UTC by snover comment:3

milestone: 1.4.3

Resetting milestone to future.

Changed January 27, 2011 12:03PM UTC by da__smith@hotmail.com comment:4

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@hotmail.com

Changed April 01, 2011 12:42AM UTC by dmethvin comment:5

Another test case from dup #4556:

http://jsfiddle.net/boushley/YxunV/1/

Changed April 01, 2011 12:43AM UTC by dmethvin comment:6

#4556 is a duplicate of this ticket.

Changed April 15, 2011 07:54AM UTC by tomgrohl comment:7

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.

Changed April 17, 2011 05:37PM UTC by john comment:8

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.

Changed April 17, 2011 07:58PM UTC by tomgrohl comment:9

Replying to [comment:8 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. :)

Changed January 04, 2014 08:04PM UTC by dmethvin comment:10

#14669 is a duplicate of this ticket.