Scripting API
Input
Keyboard
is_key_down
Check if a key is currently held down.
bool is_key_down(ScriptContext* ctx, int key_code);
Returns true every frame the key is held.
is_key_pressed
Check if a key was pressed this frame (single trigger).
bool is_key_pressed(ScriptContext* ctx, int key_code);
Returns true only on the frame the key transitions from up to down.
Common Key Codes
| Key | Code | Key | Code |
|---|---|---|---|
| W | 87 | Space | 32 |
| A | 65 | Left Shift | 340 |
| S | 83 | Left Ctrl | 341 |
| D | 68 | Escape | 256 |
| E | 69 | Enter | 257 |
| Q | 81 | Tab | 258 |
| F | 70 | Backspace | 259 |
| 0-9 | 48-57 | F1-F12 | 290-301 |
| Arrow Up | 265 | Arrow Down | 264 |
| Arrow Left | 263 | Arrow Right | 262 |
Key codes follow the GLFW convention. Cast the KeyCode enum to int.
Mouse
get_mouse_position
Get the mouse position in window coordinates.
void get_mouse_position(ScriptContext* ctx, float* out_x, float* out_y);
get_mouse_delta
Get per-frame mouse movement in raw pixels. Returns zero on the first frame after cursor capture.
void get_mouse_delta(ScriptContext* ctx, float* out_dx, float* out_dy);
is_mouse_button_down
Check if a mouse button is currently held.
bool is_mouse_button_down(ScriptContext* ctx, int button_code);
| Button | Code |
|---|---|
| Left | 0 |
| Right | 1 |
| Middle | 2 |
Cursor Capture
set_cursor_captured
Lock or unlock the cursor for FPS-style mouse look. When captured, the cursor is hidden and mouse deltas are raw movement values.
void set_cursor_captured(ScriptContext* ctx, bool captured);
is_cursor_captured
Query the current cursor capture state.
bool is_cursor_captured(ScriptContext* ctx);
Example: FPS Mouse Look
static float yaw = 0, pitch = 0;
static const float sensitivity = 0.003f;
void OnStart(ScriptContext* ctx) {
ctx->set_cursor_captured(ctx, true);
}
void OnUpdate(ScriptContext* ctx) {
// Toggle cursor capture with Escape
if (ctx->is_key_pressed(ctx, 256)) {
bool captured = ctx->is_cursor_captured(ctx);
ctx->set_cursor_captured(ctx, !captured);
}
if (!ctx->is_cursor_captured(ctx)) return;
float dx, dy;
ctx->get_mouse_delta(ctx, &dx, &dy);
yaw -= dx * sensitivity;
pitch -= dy * sensitivity;
// Clamp pitch
if (pitch > 1.5f) pitch = 1.5f;
if (pitch < -1.5f) pitch = -1.5f;
// Convert euler to quaternion (yaw then pitch)
float cy = cosf(yaw * 0.5f), sy = sinf(yaw * 0.5f);
float cp = cosf(pitch * 0.5f), sp = sinf(pitch * 0.5f);
float rot[4] = {
cy * sp, // x
sy * cp, // y
-sy * sp, // z
cy * cp // w
};
ctx->set_rotation(ctx->self, rot);
}
Example: WASD Movement
void OnUpdate(ScriptContext* ctx) {
float dt = ctx->get_delta_time();
float speed = 5.0f;
float pos[3];
ctx->get_position(ctx->self, pos);
if (ctx->is_key_down(ctx, 87)) pos[2] -= speed * dt; // W - forward
if (ctx->is_key_down(ctx, 83)) pos[2] += speed * dt; // S - backward
if (ctx->is_key_down(ctx, 65)) pos[0] -= speed * dt; // A - left
if (ctx->is_key_down(ctx, 68)) pos[0] += speed * dt; // D - right
ctx->set_position(ctx->self, pos);
}