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.

LWXGL dispatches keyboard, mouse, and window-close events to user-supplied callbacks registered with the GEventAttach* functions. Each function stores a single function pointer; calling the same attach function twice replaces the previous callback. All callbacks are invoked from within GHandleWindowEvents, which must be called each frame (either directly or via GSimpleWindowLoop).

GEventAttachKey

void GEventAttachKey(void (*Key)(int key))
Registers a callback to be invoked on each key-press event. The callback receives the character code of the pressed key.
Key
void (*)(int key)
required
Function called on key-press. key is the ASCII value of printable characters, or one of the LWXGL key constants for special keys.LWXGL key constants:
ConstantValueKey
LWXGL_KEY_LEFT170Left arrow
LWXGL_KEY_RIGHT171Right arrow
LWXGL_KEY_UP172Up arrow
LWXGL_KEY_DOWN173Down arrow
LWXGL_KEY_FN + 1 through LWXGL_KEY_FN + 12151162F1–F12
LWXGL_KEY_FN is defined as 150; function key Fn maps to LWXGL_KEY_FN + n.
The callback is not invoked when:
  • A modal dialog is currently open (GQueryModalOpen() returns 1).
  • The cursor is inside a focused input element, which consumes printable characters and Backspace.
  • The cursor is inside a console element and the key is Space (which scrolls the console to the bottom).
F12 is reserved by GSimpleWindowLoop to toggle the debug overlay and is never forwarded to the key callback. Ctrl+Escape triggers GDeleteWindow and is also not forwarded.

GEventAttachClick

void GEventAttachClick(void (*Click)(int x, int y, int btn))
Registers a callback to be invoked on each mouse button release event.
Click
void (*)(int x, int y, int btn)
required
Function called on mouse button release.
  • x, y — cursor position in window coordinates at the time of release.
  • btn — button identifier:
ValueButton
1Left
2Middle
3Right
4Scroll up
5Scroll down
The callback is not invoked when:
  • A modal dialog is currently open.
  • The release lands inside a button widget (button’s onclick is called instead and event propagation stops).
  • The release lands inside a checkbox widget (its checked state is toggled instead).
  • The release lands inside a console widget with scroll buttons 4 or 5 (the console scrolls instead).

GEventAttachDelete

void GEventAttachDelete(int (*on_exit)(void))
Registers a callback to be invoked when the user clicks the window’s close (×) button (or when GDeleteWindow is called programmatically).
on_exit
int (*)(void)
required
Function called when a window-close is requested. Return 1 to allow the window to close; return 0 to cancel and keep the window open. If this callback is not registered, close always proceeds immediately.
int on_exit(void) {
    // Prompt the user before allowing close
    GSpawnModal(1, "Quit the application?", NULL);
    return 0; // cancel immediate close; let the modal handle it
}

GEventAttachDelete(on_exit);

Full example

#include "libLWXGL.h"
#include <stdio.h>

void on_key(int key) {
    if (key == LWXGL_KEY_UP)    printf("Up arrow\n");
    if (key == LWXGL_KEY_DOWN)  printf("Down arrow\n");
    if (key == 'q')             GDeleteWindow();

    // F5 = LWXGL_KEY_FN + 5 = 155
    if (key == LWXGL_KEY_FN + 5) printf("F5 pressed\n");
}

void on_click(int x, int y, int btn) {
    printf("Click at (%d, %d) btn=%d\n", x, y, btn);
}

int on_close(void) {
    printf("Close requested\n");
    return 1; // allow close
}

int main(void) {
    GCreateWindow(800, 600, "Event Demo", 0);

    GEventAttachKey(on_key);
    GEventAttachClick(on_click);
    GEventAttachDelete(on_close);

    GSimpleWindowLoop(60, NULL);
    GTerminateWindow();
    return 0;
}

Build docs developers (and LLMs) love