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 text input elements allow users to type freeform text directly in the window. An input field becomes active whenever the mouse cursor is positioned over it — there is no explicit focus click required. Typed printable characters are appended to the field’s internal buffer, and an underscore cursor is drawn at the end to indicate the active state. The nibble-packed color byte convention (shared with buttons) controls the inactive and hover visual states independently.

GCreateInput

void GCreateInput(int id, int x, int y, int w, int h, int u, int hvr, int max);
Creates a text input element, or replaces any existing element at the given id. The internal character buffer is always 128 bytes and is zero-initialized on creation. Characters in the printable ASCII range (codes 32–126) are accepted; Backspace (code 8) removes the last character. Input is captured when the mouse cursor is inside the element bounds and no modal dialog is open.
id
int
required
Element slot index. If an element already exists at this ID it is freed and replaced.
x
int
required
X coordinate of the input field’s top-left corner.
y
int
required
Y coordinate of the input field’s top-left corner.
w
int
required
Width of the input field in pixels. Pass -1 to auto-size: the width is calculated as (max + 1) * 9 + 10, which fits exactly max characters in the 9x15 font with padding and room for the cursor indicator.
h
int
required
Height of the input field in pixels. A height of 2228 pixels is typical for a single-line field using the 9x15 font.
u
int
required
Nibble-packed color byte for the inactive state (cursor not over the field). Low nibble = fill index, high nibble = border index. See the Button & Checkbox page for a full explanation of nibble packing.
hvr
int
required
Nibble-packed color byte for the hover / active state (cursor over the field). Low nibble = fill index, high nibble = border index.
max
int
required
Maximum number of characters the field will accept. The internal buffer is always 128 bytes total, so max must be ≤ 127 to leave room for the null terminator.
Setting max greater than 127 will allow the input logic to write past the end of the 128-byte internal buffer, causing undefined behavior. Always keep max ≤ 127.
Input is hover-driven: whichever input element the cursor is over at render time receives keyboard events. If two input elements overlap, the one with the lowest slot index that overlaps the cursor takes input (elements are scanned in index order).

GGetInput

char* GGetInput(int id);
Returns a pointer to the internal null-terminated character buffer for the input element at the given id. You can read or inspect this string at any time — for example, in a button’s onclick handler or in the per-frame GSimpleWindowLoop callback.
id
int
required
Element slot index of the input field to query.
Return value: A char* pointing directly into the element’s internal 128-byte buffer. The string is always null-terminated. The pointer remains valid until the element is deleted with GDeleteElement. This is not a copy — modifications through this pointer write directly into the element’s storage.
Do not free() the returned pointer. It points into memory owned by the element and will be released automatically when the element is deleted.
To clear an input field programmatically, call GCreateInput again with the same id and parameters — this reinitializes the buffer to all zeros. Alternatively, memset the returned pointer directly for the number of bytes currently in use (up to 128 total).

Example

The example below creates a name input field and a submit button. The button’s onclick handler reads the current input value and prints it. The input auto-sizes its width to fit 31 characters using w=-1.
#include <stdio.h>
#include <string.h>
#include "libLWXGL.h"

#define ID_NAME_INPUT   0
#define ID_SUBMIT_BTN   1

void on_submit(void) {
    char *name = GGetInput(ID_NAME_INPUT);

    if (strlen(name) == 0) {
        printf("Please enter a name.\n");
        return;
    }

    printf("Hello, %s!\n", name);
}

int main(void) {
    GCreateWindow(500, 160, "Input Demo", 0);

    /* Label above the input */
    GCreateText(2, 30, 30, 15, "Enter your name:");

    /* Input field: auto-width for up to 31 chars, height=24.
       u   = border=7 (light gray), fill=1 (dark background)  -> 0x71
       hvr = border=10 (bright green), fill=1                 -> 0xA1 */
    GCreateInput(ID_NAME_INPUT,
                 30, 52,
                 -1, 24,        /* w=-1 auto-size, h=24       */
                 (7  << 4) | 1, /* u:   border=7,  fill=1     */
                 (10 << 4) | 1, /* hvr: border=10, fill=1     */
                 31);           /* max 31 characters          */

    /* Submit button */
    GCreateButton(ID_SUBMIT_BTN,
                  30, 90, 100, 26,
                  (7 << 4) | 8,
                  (8 << 4) | 9,
                  (5 << 4) | 6,
                  "Submit",
                  on_submit);

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

Build docs developers (and LLMs) love