GetKey
From Spheriki
Returns the first key in the key buffer. If there are no keys, Sphere waits until there is a key in the buffer.
Contents |
[edit]
Usage
number GetKey();
[edit]
Examples
GetKey();
This just waits for a key press from the player.
GetSystemFont().drawText(0, 0, "Press key A.");
FlipScreen();
var key = GetKey();
if (key == KEY_A)
{
Abort("You pressed A! You can follow instructions!\n");
}
else
{
Abort("You didn't press A...\n");
}
This waits for a key press, and exits with a different message depending on whether or not the player pressed 'a' on their keyboard.
var font = GetSystemFont();
var h = font.getHeight();
var text = "";
var key;
var shift = false;
while (key != KEY_ESCAPE)
{
font.drawText(0, 0, "Type in stuff. Press ESC when done.");
font.drawTextBox(0, h, 320, 240 - h, 0, text);
FlipScreen();
// Get the key
key = GetKey();
shift = IsKeyPressed(KEY_SHIFT);
switch (key)
{
case KEY_ENTER:
text += '\n';
break;
case KEY_BACKSPACE:
if (text.length > 0) text = text.substr(0, text.length - 1);
break;
case KEY_ESCAPE:
// Ignore
break;
default:
var x = GetKeyString(key, shift);
if (x != "") text += x;
}
}
A rudimentary text input box. Note that if you hold down a key, it will register repeatedly, just like in a text editing component in a GUI environment.
[edit]
Notes
- If you wish to check if a certain key has been typed, then compare the return value of GetKey() with one of the keys in the list of keycode constants (or
keys.txtin yoursphere/docsdirectory).
- To retrieve the character string that matches the key obtained from GetKey(), use the GetKeyString(keycode, shift) function.
- This function checks the key buffer, and is best used when the player is pressing keys or typing input. To check if a key is being held down or not, use the IsKeyPressed() function instead.
[edit]

