One day computers will be controlled with the power of our minds. Until that point in time, the main way of interacting with the computer is via the keyboard and mouse.
Keyhandling differs between browsers, therefore Gamma provides some convenience methods that the game developer can use to create keybindings that work across different browsers. This is provided by the gma.keyHandler object.
The register function allows you to register a function to be called, when a particular button is pressed.
Note
You need to register against a keycode like 65, rather than a so a list of common keycodes is listed at the end of this page.
When the button is pressed (or released), the function will be called with the event object that is generated by the keypress. This event object can used to determine whether the key was pressed or released.
gma.keyHandler.register(65, function(event) {
if (event.type==="keyup") {
alert(You pressed a);
}
});
The keyHandler also has reset and numEvents functions. The reset function can be used to remove all previously registered bindings and the numEvents method will give you the number of bindings registered for a particular key.
// This will say 0 because nothing is registered for code 70 yet
alert(gma.keyHandler.numEvents(70));
// Set a binding for the e button
gma.keyHandler.register(70, function(event) { alert(You pressed e);});
// Now it will say 1 because we just registered one
alert(gma.keyHandler.numEvents(70));
// Remove all currently registered bindings
gma.keyHandler.reset()
// And it will say 0 again, because we removed all the bindings
alert(gma.keyHandler.numEvents(70));