@keyListener: a TypeScript keyboard event filter
Alas, detecting which keyboard key was pressed through JavaScript is a bit of mess. There’s KeyboardEvent.key, which is supposed to clear all this up, but that’s not going to do us a lot of good now because it’s not widely supported yet. So until support improves (and even after that) it might be a good idea to tuck away the filtering of keyboard events from keys we’re not interested in and put it somewhere where we don’t have to worry about it. Like a pretty decorator for methods on our classes. Here’s my take.
I used keyCode
which although deprecated is at least widely supported. For my
limited use case (space and enter keys) the behaviour seems to be consistent
across browsers. If you need to allow for more edge cases you can of course
extend this example further.