Final Platform Layer  0.9.6-beta
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
}

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;
}
}

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;
}
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;
}

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;
}

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;
}

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
}

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!
fplMouseEventType_Button
Mouse button event.
Definition: final_platform_layer.h:5874
fplEvent::type
fplEventType type
Event type.
Definition: final_platform_layer.h:6056
fplMouseEvent::mouseX
int32_t mouseX
Mouse X-Position.
Definition: final_platform_layer.h:5902
fplMouseButtonType_Left
Left mouse button.
Definition: final_platform_layer.h:5884
fplEvent
A structure containing event data for all event types (Window, Keyboard, Mouse, etc....
Definition: final_platform_layer.h:6054
fplWindowEvent::type
fplWindowEventType type
Window event type.
Definition: final_platform_layer.h:5792
fplWindowEventType_Resized
Window has been resized.
Definition: final_platform_layer.h:5754
fplMouseButtonType_Middle
Middle mouse button.
Definition: final_platform_layer.h:5888
fplWindowEventType_Exposed
Window was exposed.
Definition: final_platform_layer.h:5768
fplEventType_Keyboard
Keyboard event.
Definition: final_platform_layer.h:6046
fplEvent::gamepad
fplGamepadEvent gamepad
Gamepad event data.
Definition: final_platform_layer.h:6065
fplMouseButtonType
fplMouseButtonType
An enumeration of mouse button types (Left, Right, ...)
Definition: final_platform_layer.h:5880
fplWindowUpdate
fpl_platform_api bool fplWindowUpdate()
Clears the internal event queue and updates input devices if needed.
fplEventType_Window
Window event.
Definition: final_platform_layer.h:6044
fplPollEvent
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 ...
fplGamepadEvent::type
fplGamepadEventType type
Gamepad event type.
Definition: final_platform_layer.h:6034
fplGamepadEventType_StateChanged
Gamepad state updated.
Definition: final_platform_layer.h:5918
fplWindowEventType_Restored
Window has been restored.
Definition: final_platform_layer.h:5764
fplMouseEventType_Wheel
Mouse wheel event.
Definition: final_platform_layer.h:5876
fplKeyboardEvent::keyCode
uint64_t keyCode
Raw ascii key code or 32-bit unicode for text input.
Definition: final_platform_layer.h:5856
fplMouseEvent::buttonState
fplButtonState buttonState
Button state.
Definition: final_platform_layer.h:5900
fplButtonState_Press
Key pressed.
Definition: final_platform_layer.h:5808
fplWindowEventType_LostFocus
Window lost focus.
Definition: final_platform_layer.h:5758
fplMouseEvent::wheelDelta
float wheelDelta
Mouse wheel delta.
Definition: final_platform_layer.h:5906
fplMouseEvent::mouseY
int32_t mouseY
Mouse Y-Position.
Definition: final_platform_layer.h:5904
fplMouseEventType_Move
Mouse position has been changed.
Definition: final_platform_layer.h:5872
fplWindowEventType_Minimized
Window has been minimized.
Definition: final_platform_layer.h:5760
fplKeyboardEvent::type
fplKeyboardEventType type
Keyboard event type.
Definition: final_platform_layer.h:5858
fplMouseButtonType_Right
Right mouse button.
Definition: final_platform_layer.h:5886
fplGamepadEventType_Disconnected
Gamepad disconnected.
Definition: final_platform_layer.h:5916
fplEvent::mouse
fplMouseEvent mouse
Mouse event data.
Definition: final_platform_layer.h:6063
fplEvent::keyboard
fplKeyboardEvent keyboard
Keyboard event data.
Definition: final_platform_layer.h:6061
fplKey
fplKey
An enumeration of mapped keys (Based on MS Virtual-Key-Codes, mostly directly mapped from ASCII)
Definition: final_platform_layer.h:5556
fplWindowEventType_Maximized
Window has been maximized.
Definition: final_platform_layer.h:5762
fplWindowEvent::dropFiles
fplWindowDropFiles dropFiles
Drop files.
Definition: final_platform_layer.h:5799
fplPollEvents
fpl_platform_api void fplPollEvents()
Polls all the events from the OS and clears the internal event queue.
fplGamepadEventType_Connected
Gamepad connected.
Definition: final_platform_layer.h:5914
fplMouseEvent::type
fplMouseEventType type
Mouse event type.
Definition: final_platform_layer.h:5896
fplEventType_Mouse
Mouse event.
Definition: final_platform_layer.h:6048
fplEventType_Gamepad
Gamepad event.
Definition: final_platform_layer.h:6050
fplEvent::window
fplWindowEvent window
Window event data.
Definition: final_platform_layer.h:6059
fplKeyboardEvent::mappedKey
fplKey mappedKey
Mapped key.
Definition: final_platform_layer.h:5864
fplButtonState
fplButtonState
An enumeration of button states.
Definition: final_platform_layer.h:5804
fplKeyboardEvent::buttonState
fplButtonState buttonState
Button state.
Definition: final_platform_layer.h:5862