Final Platform Layer 0.9.8-beta
Loading...
Searching...
No Matches
Window events

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.

fplEvent currentEvent;
while (fplPollEvent(&currentEvent)) {
// ... Handling the event
}
fpl_platform_api bool fplPollEvent(fplEvent *ev)
Polls the next event from the internal event queue or from the OS, handles them, and removes it from ...
A structure containing event data for all event types (Window, Keyboard, Mouse, etc....

Handling the Events

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

fplEvent currentEvent;
while (fplPollEvent(&currentEvent)) {
switch (currentEvent.type) {
{
// A window event, like resize, lost/got focus, etc.
switch (currentEvent.window.type) {
// ...
}
} break;
{
// A keyboard event, like key down/up, pressed, etc.
switch (currentEvent.keyboard.type) {
// ...
}
} break;
{
// A mouse event, like mouse button down/up, mouse move, etc.
switch (currentEvent.mouse.type) {
// ...
}
} break;
{
// A gamepad event, like connected/disconnected, state-updated etc.
switch (currentEvent.gamepad.type) {
// ...
}
} break;
}
}
@ fplEventType_Keyboard
Keyboard event.
@ fplEventType_Window
Window event.
@ fplEventType_Mouse
Mouse event.
@ fplEventType_Gamepad
Gamepad event.
fplMouseEvent mouse
Mouse event data.
fplKeyboardEvent keyboard
Keyboard event data.
fplWindowEvent window
Window event data.
fplGamepadEvent gamepad
Gamepad event data.
fplEventType type
Event type.
fplGamepadEventType type
Gamepad event type.
fplKeyboardEventType type
Keyboard event type.
fplMouseEventType type
Mouse event type.
fplWindowEventType type
Window event type.

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 descriped 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 non button press, you simply compare against fplButtonState_Release.

If you just want to know if a key is pressed or hold-down, just do a greater equals comparison of 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.mouse.type) {
{
fplMouseButtonType button = currentEvent.mouse.button;
fplButtonState state = currentEvent.mouse.buttonState;
bool buttonDown = state >= fplButtonState_Press;
switch (button) {
{
// Left mouse button down/released
} break;
{
// Middle mouse button down/released
} break;
{
// Right mouse button down/released
} break;
}
} break;
{
float wheelDelta = currentEvent.mouse.wheelDelta;
if (wheelDelta < 0.0f) {
// Mouse-wheel down
} else if (wheelDelta > 0.0f) {
// Mouse-wheel up
}
} break;
{
int mouseX = currentEvent.mouse.mouseX;
int mouseY = currentEvent.mouse.mouseY;
// ... do something with the mouse position
} break;
}
fplButtonState
An enumeration of button states.
fplMouseButtonType
An enumeration of mouse button types (Left, Right, ...)
@ fplButtonState_Press
Key pressed.
@ fplMouseEventType_Move
Mouse position has been changed.
@ fplMouseEventType_Button
Mouse button event.
@ fplMouseEventType_Wheel
Mouse wheel event.
@ fplMouseButtonType_Middle
Middle mouse button.
@ fplMouseButtonType_Right
Right mouse button.
@ fplMouseButtonType_Left
Left mouse button.
int32_t mouseX
Mouse X-Position.
fplButtonState buttonState
Button state.
int32_t mouseY
Mouse Y-Position.
float wheelDelta
Mouse wheel delta.
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.keyboard.type) {
case fplKeyboardEventType_KeyButton:
{
fplButtonState state = currentEvent.keyboard.buttonState;
// ... Handle the key code
uint64_t keyCode = currentEvent.keyboard.keyCode;
if (state >= fplButtonState_Pressed) {
if (keyCode == 65 || keyCode == 97) {
// Letter A is held down
}
}
// or
// ... handle the mapped key
fplKey mappedKey = currentEvent.keyboard.mappedKey;
if (state == fplButtonState_Released) {
if (mappedKey == fplKey_F1) {
// F1 key pressed
}
}
} break;
case fplKeyboardEventType_CharInput:
{
if(currentEvent.keyboard.keyCode > 0 && event.keyboard.keyCode < 0x10000) {
// Handle character input
}
} break;
}
fplKey
An enumeration of mapped keys (Based on MS Virtual-Key-Codes, mostly directly mapped from ASCII)
uint64_t keyCode
Raw ascii key code or 32-bit unicode for text input.
fplKey mappedKey
Mapped key.
fplButtonState buttonState
Button state.

Gamepad events

Gamepad event data are stored in the fplGamepadEvent structure.

switch (currentEvent.gamepad.type) {
{
// New gamepad device connected
} break;
{
// Lost connection to a gamepad device
} break;
{
// State of one controller updated (Buttons, Movement, etc.)
if (absf(currentEvent.gamepad.leftStickX) > 0) {
// ... Handle horizontal movement on left stick
}
if (currentEvent.gamepad.actionX.isDown) {
// ... X-Button is held down
}
} break;
}
@ fplGamepadEventType_StateChanged
Gamepad state updated.
@ fplGamepadEventType_Disconnected
Gamepad disconnected.
@ fplGamepadEventType_Connected
Gamepad connected.

Window events

Window event data are stored in the fplWindowEvent structure.

switch (currentEvent.window.type) {
{
uint32_t newWidth = currentEvent.window.width;
uint32_t newHeight = currentEvent.window.height;
// ... Window was resized, handle it properly
} break;
case fplWindowEventType_GetFocus:
{
// ... Do something when window lost/got focus
} break;
{
// ... 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;
{
// ... Do something when window state was changed
} break;
}
@ fplWindowEventType_Minimized
Window has been minimized.
@ fplWindowEventType_LostFocus
Window lost focus.
@ fplWindowEventType_Exposed
Window was exposed.
@ fplWindowEventType_Restored
Window has been restored.
@ fplWindowEventType_Resized
Window has been resized.
@ fplWindowEventType_Maximized
Window has been maximized.
fplWindowDropFiles dropFiles
Drop files.

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 (fplWindowUpdate()) {
// ... Your code
}
fpl_platform_api bool fplWindowUpdate()
Clears the internal event queue and updates input devices if needed.
fpl_platform_api void fplPollEvents()
Polls all the events from the OS and clears the internal event queue.

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!