Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/dRessedAlarm184/LWXGL/llms.txt

Use this file to discover all available pages before exploring further.

In addition to event callbacks registered with GEventAttachKey and GEventAttachClick, LWXGL provides polling functions to read the current mouse and keyboard state at any point within a frame. These are useful for continuous input — e.g. smooth movement — where a callback-per-event model is too coarse. State is updated by GHandleWindowEvents, so polling before that call each frame will return the state from the previous frame.

GQueryMouse

void GQueryMouse(int* x, int* y, int* btn)
Fills in the current cursor position and any currently held mouse button.
x
int*
required
Output. Set to the cursor’s current X coordinate in window pixels. Set to -1 when the cursor is outside the window.
y
int*
required
Output. Set to the cursor’s current Y coordinate in window pixels. Set to -1 when the cursor is outside the window.
btn
int*
required
Output. Set to the button currently held down (1=left, 2=middle, 3=right, 4=scroll-up, 5=scroll-down), or 0 if no button is held.
int mx, my, mbtn;
GQueryMouse(&mx, &my, &mbtn);

if (mx != -1 && my != -1) {
    printf("Cursor at (%d, %d), button held: %d\n", mx, my, mbtn);
}

GQueryKeyboard

unsigned char* GQueryKeyboard(void)
Returns a pointer to the internal unsigned char[8] array of currently held keys. Each slot contains the character code (ASCII or LWXGL key constant) of one held key, or 0 if that slot is unused. Up to 8 simultaneous keys are tracked. Returns: Pointer to an internal unsigned char[8] array. Do not free or store this pointer across frames; its contents are updated in-place by GHandleWindowEvents.
unsigned char* keys = GQueryKeyboard();
for (int i = 0; i < 8; i++) {
    if (keys[i] != 0)
        printf("Key held: %d\n", keys[i]);
}
For checking a single specific key, prefer GQueryKeyDown over iterating the array manually.

GQueryKeyDown

int GQueryKeyDown(int ch)
Returns whether a specific key is currently held down. Scans the internal 8-slot pressed-keys array for a match.
ch
int
required
The character code to check. Accepts ASCII values (e.g. 'a', ' ') or LWXGL key constants:
ConstantValueKey
LWXGL_KEY_LEFT170Left arrow
LWXGL_KEY_RIGHT171Right arrow
LWXGL_KEY_UP172Up arrow
LWXGL_KEY_DOWN173Down arrow
LWXGL_KEY_FN + n150+nF1–F12
Returns: 1 if the key is currently held, 0 otherwise.

Frame-loop example

The polling functions work naturally alongside GSimpleWindowLoop. Use GQueryKeyDown inside the per-frame callback for smooth, held-key movement:
int player_x = 100;
int player_y = 100;

void on_frame(int tick) {
    int x, y, btn;
    GQueryMouse(&x, &y, &btn);

    if (GQueryKeyDown(LWXGL_KEY_LEFT))  player_x -= 2;
    if (GQueryKeyDown(LWXGL_KEY_RIGHT)) player_x += 2;
    if (GQueryKeyDown(LWXGL_KEY_UP))    player_y -= 2;
    if (GQueryKeyDown(LWXGL_KEY_DOWN))  player_y += 2;
}

int main(void) {
    GCreateWindow(640, 480, "Input Demo", 0);
    GSimpleWindowLoop(60, on_frame);
    GTerminateWindow();
    return 0;
}
Because GHandleWindowEvents is called at the start of each frame by GSimpleWindowLoop, GQueryKeyDown always reflects the latest input state when on_frame runs.

Build docs developers (and LLMs) love