Keyboard Module (last update 13/05/01)

Description:

This module adds keyboard support to the gamelib library. As of 29/05/2001 keyboard support for UNIX platforms has been added! Thanks to Ralf Berkvens (ralf@ilse.nl) for his advice on trapping keyboard events on unix platforms. So keyboard-controlled games will now work with Windows, Mac, Sun, Linux, FreeBSD, etc etc (oh and IRIX - a little known platform still used by a small rabble of disturbingly dedicated followers :-)

To use:

The code can be linked in to your script by adding this line to the <HEAD> section of your document:

<script language="Javascript" src="gamelib_keyboard.js"></script>

The keyboard module is the easiest to use by far. To trap a key, just add something like:

upKey=Kb_trapkey("q")

From this point on, whenever the q key is pressed, it will alter a property of upKey. The case is not relevent. For games use, it's not important whether you have caps-lock on or off, and so "Q"="q". If q is pressed, upKey.pressed will equal true, otherwise it will equal false. So if your game needed to check whether q was pressed, you'd do something such as:

if(upKey.pressed){
  // do something
}

If you prefer a more Event Driven approach to coding, then you can use the new setEvent and clearEvent methods for keys. With these, you can tell a key what to do when it's pressed! So if you wanted your game/script to call the function called myFunc() when key "F" was pressed, you'd do this:

upKey=Kb_trapkey("f");
upKey.setEvent("myFunc()");

Both approaches have advantages and disadvantages. The first method is by far the best when you are already using a central loop, and can check the keys during the game. The second is better for menus etc. See the docs below for more details...

If you are developing a game for Internet Explorer only, you can also trap the ESC, CTRL and SHIFT keys, and also the LEFT,RIGHT,UP and DOWN cursor keys by passing their name into the keytrapping function. So if you wished to trap the SHIFT key, you'd use this:

shiftKey=Kb_trapkey("SHIFT")

For Netscape 4/6, the following mappings are used instead of the special keys:

UP = I, DOWN = K, LEFT = J, RIGHT = L, CTRL = Z, SHIFT = A

(What this means is, if you trap the SHIFT key, then people playing your game with Netscape will use key A instead of the shift key. This is because Netscape doesn't register key events with the special keys.)

Descriptions of methods

MethodParametersDescription
setEventString This method associates an event with a keypress. The string is usually a function to be called, but can be any valid javascript statement. Be aware that events occur when the key is pressed down, and will be triggered again if the key repeats (when held down.) It's not a good idea to use this method for real-time input in action type games. It is a good idea to use it for menus or other navigation interfaces, where you don't want to have to set up a loop, or hook into the gamelib timer.
clearEvent(none)Cancels the event associated with a key.

List of Global properties

Kb_lastkey

Descriptions of Global properties

PropertyData TypeRead/WriteDescription
Kb_lastkeyObjectR/W

This variable holds the last key pressed. It holds the key trapping object you created to make things easier for testing. So if you wanted to test if the up key (as defined above) was the last key pressed, you'd do the following:

if(Kb_lastkey==upKey){
  // do something
}