Java Script Keyboard Input

3 07 2013

Today, I found a nice little Java Script library called keypress.js. It handles Keyboard events. I used it to rewrite the keyboard handling in “Heimholung” my actual project.

Before keypress I had these handlers:

addEventListener(“keydown”, function (e) {
keysDown[e.keyCode] = true;
}, false);addEventListener(“keyup”, function (e) {
delete keysDown[e.keyCode];
}, false);

Now I use keypress:

keypress.register_combo({
“keys”: “down”,
“on_keydown” : function(e) {
keysDown[e.keyCode]=true;
},

“on_keyup” : function(e) {
delete keysDown[e.keyCode];
},

“prevent_repeat”    : true,
});

 

After a while I got everything working. Most important was to have a function to wait until a key is pressed:

 

keypress.register_combo({
“keys”: “down”,
“on_keyup”: function(e) {
keypress.reset();
nextStateInit();
currentState.state=nextState;
},
“prevent_repeat”    : true,
});