Please stop using keypress event to handle non-printable keys

Masayuki Nakano masayuki at d-toybox.com
Thu Jan 18 02:34:59 UTC 2018


Hello, everyone.

Please stop using keypress event for handling non-printable keys in new 
code when you write new code and new automated tests. Firefox will stop 
dispatching keypress events for non-printable keys for conforming to UI 
Events and struggling with web compatibility. (non-printable key means 
key combination which won't cause inputting character, e.g., arrow keys, 
backspace key and Ctrl (and/or Alt) - "A" key, etc.)

You can perhaps just use keydown event instead. KeyboardEvent.key and 
KeyboardEvent.keyCode of non-printable key events are always same. 
Difference between keydown event and keypress event is 
KeyboardEvent.charCode of printable keys (and Ctrl/Alt + printable 
keys).  So, when you need to use only key or keyCode, please use keydown 
event.  Otherwise, when you need to use charCode, please keep using 
keypress event.

Background:

We need to fix bug 968056 (*1) for web-compat issues.

Currently, Firefox dispatches keypress events for any keys except 
modifier keys. This is traditional behavior from Netscape Navigator. 
However, this is now invalid behavior from a point of view of standards 
(*2).

I'm going to start to work on the bug from next week. However, this 
requires to rewrite too many keypress event handlers in our internal 
code and our automated tests.  So, please stop using keypress event when 
you want to handle non-printable keys at least in new code.

Thanks in advance.

1: https://bugzilla.mozilla.org/show_bug.cgi?id=968056
2: https://w3c.github.io/uievents/#legacy-keyboardevent-event-types

-- 
Masayuki Nakano <masayuki at d-toybox.com>
Software Engineer, Mozilla


More information about the firefox-dev mailing list