BindKey
From Spheriki
Sets up scripts to run when a key is held down and/or released.
Contents |
[edit]
Usage
BindKey(key, on_key_down, on_key_up);
- key The keycode constant for the key you want to bind. See list of keycode constants or
docs/keys.txtin your Sphere installation for possible values. - on_key_down String, containing the script to run when the key is pressed down.
- on_key_up String, containing the script to run when the key is released.
[edit]
Example
To set a variable mode to rely on the state of the spacebar:
BindKey(KEY_SPACE, "mode = 'in';", "mode = 'out';");
Note that the JavaScript for the on_key_down/up scripts is inside a string.
Here is a script that allows you to test the behaviour of BindKey(). You will need to create a map named "test.rmp", though it needn't contain anything:
var haha = 0; // Increases by one for each key press var lala = 0; // Increases by one for each key release var font = GetSystemFont(); var h = font.getHeight(); function game() { BindKey(KEY_SPACE, "++haha;", "++lala"); SetRenderScript("OnRender();"); MapEngine("test.rmp", 60); } function OnRender() { font.drawText(0, 0, "haha = " + haha); font.drawText(0, h, "lala = " + lala); font.drawText(0, h * 3, "Press ESC to end."); }
[edit]
Notes
- Scripts that are bound like this will only run while the Sphere map engine is running.
- Since the scripts to handle the key events are strings, errors within them won't show up until they are encountered. This is unlike the rest of Sphere's scripts, which can be checked before the game runs.
- To remove binding from a key, use the UnbindKey() function.
- The
on_key_downscript is run once when the key is pressed down. Theon_key_upscript is run once when the key is released.
[edit]

