We use keymaster.js to power the keyboard shortcuts on a new product under development at work. It’s a great little javascript library for detecting key-presses from users.
For this product we wanted to capture the “+” key for when users wanted to Add something to their console. The confusion for me came from the fact the the “+” is used in the key() method for joining sequences of keys to get multi-key dispatching (e.g. pressing Control and Return at the same time).
It’s a pretty simple solution, but I couldn’t find it anywhere so thought I’d post here just in case someone else wants to do the same thing. As it turns out, “=” and “+” share the same key code in javascript, so the following will allow capturing of the plus key from both the numeric keypad as well as on the standard keyboard.
<script type="text/javascript" src="keymaster.min.js"></script> <script type="text/javascript"> (function() { var myFunc = function() { console.log("User hit the + key"); }; key('=,shift+=', myFunc); }()); </script> |
An unfortunate by-product of the above, however, is that the same function will fire if the user hits just the “=” key. A pretty trivial trade-off IMHO. If anyone can think of a way to differentiate between the two let me know!