Detects when a key is first pressed (single trigger). Perfect for actions that should only happen once:
if (Raylib.isKeyPressed(KeyCode.KEY_SPACE)) { // Jump or shoot - triggers once per press player.jump()}if (Raylib.isKeyPressed(KeyCode.KEY_ENTER)) { currentScreen = GameScreen.GamePlay}
isKeyPressed() only returns true on the frame when the key is first pressed, not while it’s held down.
KeyCode.KEY_SPACEKeyCode.KEY_ENTERKeyCode.KEY_ESCAPE// Arrow keysKeyCode.KEY_UPKeyCode.KEY_DOWNKeyCode.KEY_LEFTKeyCode.KEY_RIGHT// Letter keysKeyCode.KEY_AKeyCode.KEY_DKeyCode.KEY_SKeyCode.KEY_W// Number keysKeyCode.KEY_ZERO through KeyCode.KEY_NINE
The asteroid game demonstrates both types of input handling:
1
Player rotation with continuous input
update() { if (!_gameOver) { // Rotation with arrow keys if (Raylib.isKeyDown(KeyCode.KEY_LEFT)) { _player.rotation = _player.rotation - 5 } if (Raylib.isKeyDown(KeyCode.KEY_RIGHT)) { _player.rotation = _player.rotation + 5 } }}
2
Movement acceleration
// Calculate speed based on rotation_player.speed.x = Math.sin(_player.rotation * DEG2RAD) * PLAYER_SPEED_player.speed.y = Math.cos(_player.rotation * DEG2RAD) * PLAYER_SPEED// Apply thrust with UP keyif (Raylib.isKeyDown(KeyCode.KEY_UP)) { if (_player.acceleration < 1) { _player.acceleration = _player.acceleration + 0.04 }} else { // Decelerate when key is released if (_player.acceleration > 0) { _player.acceleration = _player.acceleration - 0.02 } else if (_player.acceleration < 0) { _player.acceleration = 0 }}
3
Apply movement to position
// Update position based on speed and acceleration_player.position.x = _player.position.x + (_player.speed.x * _player.acceleration)_player.position.y = _player.position.y - (_player.speed.y * _player.acceleration)
import "raylib" for MouseButton// Check if button is pressed (continuous)if (Raylib.isMouseButtonDown(MouseButton.MOUSE_LEFT_BUTTON)) { // Left mouse button is held down}// Check if button was just clicked (single)if (Raylib.isMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON)) { // Left mouse button was just clicked}// Check if button was releasedif (Raylib.isMouseButtonReleased(MouseButton.MOUSE_LEFT_BUTTON)) { // Left mouse button was just released}
// Check if key is releasedRaylib.isKeyReleased(KeyCode.KEY_SPACE)// Check if key is up (not pressed)Raylib.isKeyUp(KeyCode.KEY_SPACE)// Get the last key pressedvar key = Raylib.getKeyPressed()// Get character pressed (for text input)var char = Raylib.getCharPressed()