Creating a keyboard events manager in JavaScript, without using switch or if..else-if
1 min readMar 3, 2016
I was asked by a friend of mine if I knew of an elegant approach to handling keyboard events without having to use the switch statement or the if..else-if hell. I remembered that I used to use this pattern where I would keep the keyboard events as the keys of an object and add any extra data on those keys, like the handler, the type of event, name of the key that was pressed, etc. This also works well when developing a keyboard shortcut manager.
Enjoy!