BindKey

From Spheriki

Jump to: navigation, search

Sets up scripts to run when a key is held down and/or released.


Contents

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.txt in 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.


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.");
}


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_down script is run once when the key is pressed down. The on_key_up script is run once when the key is released.


See also

Personal tools