Even better normalization of KeyboardEvent + MouseEvent#776
Conversation
There was a problem hiding this comment.
Will .key be '' ever here?
There was a problem hiding this comment.
@spicyj It's either undefined because it isn't supported, or it may be (according to the standard) initialized to '', which I take would be equivalent to unsupported (but recognized). '' is NOT a valid key, it's either recognized or 'Unindentified'.
There was a problem hiding this comment.
In that case, your ending return '' doesn't make sense to me. But perhaps that's never reached? If not, I'd just do invariant(false); there.
There was a problem hiding this comment.
@spicyj Correct, invariant(false) is better too!
|
Mind editing the docs to include getModifierState? |
|
@spicyj Done, also added Also moved out the functions from the |
There was a problem hiding this comment.
Can you rebase on 182a237? In particular, move some of this logic into getEventKey.
|
@yungsters Done. I also moved my As I have not tested this updated PR yet, will do when I get home later tonight. PS. FYI when |
There was a problem hiding this comment.
Nit: Add a colon after type.
Hah, I was thinking the same thing. Most of the other events have these functions inline. In my personal opinion, seeing a line reading
I think your PR adds the only caller to |
|
@yungsters Done. Also, you're right, I added that, I must've confused myself when I rebased. |
There was a problem hiding this comment.
This can be non-display characters, so shouldn't this be something like:
return translateToKey[charCode] || String.fromCharCode(charCode);
...or something? Listening to onKeyPress and checking event.key === 'Enter' should work. Currently, this will just return an empty string:
> String.fromCharCode(13)
""
There was a problem hiding this comment.
@yungsters That's keyPress so charCode is actually the actual character code, and not virtual key code. keyup and keydown are for virtual key codes. Also, while FireFox does generate keypress for some non-printable keys, only it seems to do it, that's why I've added an exception to the SimpleEventPlugin to discard non-printable keys for keypress, to normalize the behavior.
There was a problem hiding this comment.
Hmm... I'm seeing that keyCode and charCode are both set for "Enter" in Chrome on keypress. Are you seeing something different?
There was a problem hiding this comment.
@yungsters Yep, but they both contain the same value, I have a comment a bit further down in which about it. Basically, charCode is correct for keypress and keyCode is correct for keydown and keyup, but depending on browser, either of them can be null or the same as the other. This PR also normalizes this behavior so that only one of them is set.
Also, enter is special because it actually produces a character.
There was a problem hiding this comment.
I think your normalization of of charCode and keyCode are fine. However, my understanding of the spec is that key should be "Enter" when pressing Enter on all keydown, keyup, and keypress events. The code here will never set key to "Enter".
By the way, I've been using this to see what browsers do: http://jsfiddle.net/yungsters/D9LjL/
There was a problem hiding this comment.
@yungsters Yeah I've been using something similar, but neat! Anyway, I pushed a new change, using translateToKey is broken, as it would actually translate actual characters into arbitrary keys (charCode != keyCode). Proper fix should be to treat enter as a special-case.
There was a problem hiding this comment.
Since keypress is deprecated, not sure it's worth worrying too much about what key is for it.
|
@yungsters how do you feel about this? |
|
@salier Was a bit involved too I think? (or rather, was working on his own implementation before he saw this) |
|
I'm going to target 0.10 for this since it doesn't seem likely this is going in in the super immediate future. |
KeyboardEvent now normalizes "charCode", "keyCode", "which" across all browsers KeyboardEvent has partial "key"-support for KeyDown/KeyUp and full "key"-support for KeyPress. KeyboardEvent, MouseEvent and TouchEvent now has "getModifierState", polyfill when not implemented.
Even better normalization of KeyboardEvent + MouseEvent
KeyboardEventnow normalizescharCode,keyCode,whichacross all browsers, partialkey-support for KeyDown/KeyUp, fullkey-support for KeyPress.getModifierStateis added to the interface ofKeyboardEvent,MouseEventandTouchEventand is polyfilled for IE8.TouchEventdoesn't supportgetModifierStateaccording to the latest draft, but considering that the interface is otherwise identical in the design, it would be exceptionally inconsistent for it to be intentionally omitted.whichis redundant and not very precisely defined, but it is in the legacy standard. I also took the liberty to removecharfrom the interface, it existed briefly in the working draft before it got dropped. It was only ever supported by IE9, which makes it practically useless.I've also extensively documented all the nuances.
Fixes #706.