alexmingoia / purescript-pux

Build type-safe web apps with PureScript.
https://www.purescript-pux.org
Other
566 stars 76 forks source link

onKey* events should use KeyboardEvent in handlers #135

Closed esad closed 7 years ago

esad commented 7 years ago

Right now, a generic Event is passed, which for any kind of meaningful handling (comparing key codes etc.) needs to be converted to KeyboardEvent using eventToKeyboardEvent which wraps it in an F. Is there really a possibility of eventToKeyboardEvent failing for a keyboard event?

esad commented 7 years ago

I realized that KeyboardEvent is quite new and supported only in recent browser versions, so this conversion really may fail. However in this case, there is no possibility of accessing keyCode in Event without doing someunsafeCoerce like in https://github.com/quephird/space-invaders-ps/blob/master/src/Handlers/Keyboard.purs#L73

alexmingoia commented 7 years ago

At runtime, any JavaScript function can trigger DOM events with arbitrary arguments, and that is partly why there is always a possibility an event listener will not receive a KeyboardEvent.

eventToKeyboardEvent is the safest way but in most contexts I think unsafeCoerce is a fine way to go.