From Spherical
Revision as of 21:58, 1 June 2013 by Apollolux (talk | contribs) (created from http://web.archive.org/web/20111228001515/http://www.spheredev.org/wiki/IsKeyPressed)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Check if a certain key is being held down on the keyboard.


boolean IsKeyPressed(key);


if (IsKeyPressed(KEY_ENTER))

IsKeyPressed() is often used in conditions and loops. The above will check if the Enter key was pressed, and if so, it will run the condition.

You can use !IsKeyPressed to negate the function: if the key was NOT pressed.

if (!IsKeyPressed(KEY_ENTER))
  // ...


  • This function does not halt the engine like GetKey() does, and can therefore be used in continuous loops.
  • Do not use this function to check for key strokes. This function doesn't use a key queue, so it's suited for controlling a spaceship or a person on a map. However, if you need to pick up every key stroke (e.g. for a text box), use GetKey() instead.
  • If you need to find out if any key is being pressed, it's better to use the IsAnyKeyPressed() function.

See also