Skip to content

Window events 2

Finalspace edited this page May 29, 2026 · 2 revisions

Table of Contents

Polling the window events

Call fplPollEvent() in a while-loop inside your actual main-loop with a pointer to a fplEvent argument to poll the next event from the OS event queue.

Each event is translated into the fplEvent argument which you can handle or not. If there are no events left, the function returns false.

 currentEvent;
while ((&currentEvent)) {
    // ... Handling the event
}

Handling the Events

Each event has a fplEvent::type field which you can check to read the actual data (Keyboard, Mouse, Window, etc.).

 currentEvent;
while ((&currentEvent)) {
    switch (currentEvent.) {
        case :
        {
            // A window event, like resize, lost/got focus, etc.
            switch (currentEvent..) {
                // ...
            }
        } break;
        case :
        {
            // A keyboard event, like key down/up, pressed, etc.
            switch (currentEvent..) {
                // ...
            }
        } break;
        case :
        {
            // A mouse event, like mouse button down/up, mouse move, etc.
            switch (currentEvent..) {
                // ...
            }
        } break;
        case :
        {
            // A gamepad event, like connected/disconnected, state-updated etc.
            switch (currentEvent..) {
                // ...
            }
        } break;
    }
}

All available event types are stored in the fplEventType enumeration.

Handle the event data

All relevant event data are stored in fields that match the lowercase fplEventType name.

Each event structure has another type field to check for the actual type (Key-Down, Mouse-Move, Window-Resize, etc.).

Button states

All mouse/keyboard or gamepad button states are described as a fplButtonState.

To detect a non-repeatable button press, you simply compare against fplButtonState_Press.

To detect a repeatable button press, you simply compare against fplButtonState_Repeat.

To detect a button release, you simply compare against fplButtonState_Release.

If you just want to know if a key is pressed or held down, just do a greater-or-equal comparison against fplButtonState_Press and you are golden.

Mouse events

Mouse event data are stored in the fplMouseEvent structure.

A mouse event contains either a single button click/release or a mouse wheel change or a position change.

switch (currentEvent..) {
    case :
    {
         button = currentEvent..button;
         state = currentEvent..;
        bool buttonDown = state >= ;
        switch (button) {
            case :
            {
                // Left mouse button down/released
            } break;
            case :
            {
                // Middle mouse button down/released
            } break;
            case :
            {
                // Right mouse button down/released
            } break;
        }
    } break;

    case :
    {
        float wheelDelta = currentEvent..;
        if (wheelDelta < 0.0f) {
            // Mouse-wheel down
        } else if (wheelDelta > 0.0f) {
            // Mouse-wheel up
        }
    } break;

    case :
    {
        int mouseX = currentEvent..;
        int mouseY = currentEvent..;
        // ... do something with the mouse position
    } break;
}

Note: Any mouse button event contains the position of the click as well.

Keyboard events

Keyboard event data are stored in the fplKeyboardEvent structure.

You can either check for the original fplKeyboardEvent::keyCode or use the fplKeyboardEvent.mappedKey field - which is much easier and less error-prone.

switch (currentEvent..) {
    case fplKeyboardEventType_KeyButton:
    {
         state = currentEvent..;

        // ... Handle the key code
        uint64_t keyCode = currentEvent..;
        if (state >= fplButtonState_Pressed) {
            if (keyCode == 65 || keyCode == 97) {
                // Letter A is held down
            }
        }

        // or

        // ... handle the mapped key
         mappedKey = currentEvent..;
        if (state == fplButtonState_Released) {
            if (mappedKey == ) {
                // F1 key pressed
            }
        }
    } break;

    case fplKeyboardEventType_CharInput:
    {
        if(currentEvent.. > 0 && event.keyboard.keyCode < 0x10000) {
            // Handle character input
        }
    } break;
}

Gamepad events

Gamepad event data are stored in the fplGamepadEvent structure.

switch (currentEvent..) {
    case :
    {
        // New gamepad device connected
    } break;

    case :
    {
        // Lost connection to a gamepad device
    } break;

    case :
    {
        // State of one controller updated (Buttons, Movement, etc.)
        if (absf(currentEvent..leftStickX) > 0) {
            // ... Handle horizontal movement on left stick
        }
        if (currentEvent..actionX.isDown) {
            // ... X-Button is held down
        }
    } break;
}

Window events

Window event data are stored in the fplWindowEvent structure.

switch (currentEvent..) {
    case :
    {
        uint32_t newWidth = currentEvent..width;
        uint32_t newHeight = currentEvent..height;
        // ... Window was resized, handle it properly
    } break;

    case fplWindowEventType_GetFocus:
    case :
    {
        // ... Do something when window lost/got focus
    } break;

    case :
    {
        // ... Do something when window was exposed
    } break;

    case fplWindowEventType_DropSingleFile:
    {
        const char *filePath = ev.window.dropFiles.single.filePath;
        // ... Do something when window got a single file dropped into it
    } break;

    case :
    case :
    case :
    {
        // ... Do something when window state was changed
    } break;
}

Ignoring the events

When you don't care about any events you can simply call fplPollEvents() to process the events and be done with them.

while (()) {
    ();

    // ... Your code
}

Important Notes

Note: FPL does not cache the events from the previous update. If you don't handle or cache the event - the data is lost!

Final Platform Layer

Pages

Topics

Data Structures

Clone this wiki locally