I'm writing a simple game in JavaScript, which already handles basic keyboard input like so:
var input = {};
while (!done) {
handleInput(input);
update();
render();
}
document.onkeydown = function(e) {
input[e.keyCode] = true;
}
document.onkeyup = function(e) {
input[e.keyCode] = false;
}
Now I need the game to handle key combos (like CTRL+X for example). I would like it to accept such combos on keyup
only.
Two possible solutions that come into my mind are:
- exposing an array containing a list of
keyup
events (object with "main" key plus modifiers). ThehandleInput
function would be responsible for draining the queue every time it polls it - keeping track of possible key combos inside
handleInput
(watching for held down modifier keys) and trigger the combo behavior when the "main" key goes up (I actually don't like this that much)
Would you suggest me an elegant way to extend the current functionality?