-
Notifications
You must be signed in to change notification settings - Fork 13
Window events 2
- Polling the window events
- Handling the Events
- Handle the event data
- Ignoring the events
- Important Notes
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 ((¤tEvent)) {
// ... Handling the event
}Each event has a fplEvent::type field which you can check to read the actual data (Keyboard, Mouse, Window, etc.).
currentEvent;
while ((¤tEvent)) {
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.
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.).
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 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 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 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 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;
}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
}Note: FPL does not cache the events from the previous update. If you don't handle or cache the event - the data is lost!
- Assertion & Debug
- Atomic operations
- Audio functions
- Clipboard functions
- Console functions
- Constants
- Display/Monitor functions
- Dynamic library loading
- Error Handling
- Files/IO functions
- Function macros
- Hardware Infos
- Input types and functions
- Localization functions
- Logging
- Memory Macros
- Memory functions
- Operating system Infos
- Path functions
- Platform functions
- Session Infos
- Settings & Configurations
- Storage class identifiers
- String functions
- Threading and synchronizations routines
- Timing functions
- Video functions
- Window events
- Window functions
- fplARMCPUCapabilities
- fplAudioChannelMap
- fplAudioDeviceID
- fplAudioDeviceInfo
- fplAudioFormat
- fplAudioSettings
- fplColor32
- fplConditionVariable
- fplConsoleSettings
- fplCPUCapabilities
- fplCPUIDLeaf
- fplDateTime
- fplDateTimeCreationResult
- fplDateTimeResult
- fplDisplayInfo
- fplDisplayMode
- fplDynamicLibraryHandle
- fplEndianess
- fplEvent
- fplFileEntry
- fplFileHandle
- fplFilePermissions
- fplFileTimeStamps
- fplGamepadButton
- fplGamepadData
- fplGamepadEvent
- fplGamepadInfo
- fplGamepadInputBinding
- fplGamepadMapping
- fplGamepadSettings
- fplGamepadState
- fplGamepadStates
- fplGraphicsApiSettings
- fplImageSource
- fplInputBackendMask
- fplInputBackendSupport
- fplInputDevice
- fplInputDeviceGuid
- fplInputSettings
- fplInternalConditionVariable
- fplInternalDynamicLibraryHandle
- fplInternalFileEntryHandle
- fplInternalFileHandle
- fplInternalFileRootInfo
- fplInternalMutexHandle
- fplInternalSemaphoreHandle
- fplInternalSignalHandle
- fplInternalThreadHandle
- fplKeyboardEvent
- fplKeyboardState
- fplLogSettings
- fplLogWriter
- fplLogWriterConsole
- fplLogWriterCustom
- fplMemoryAllocationSettings
- fplMemoryBlock
- fplMemoryInfos
- fplMemorySettings
- fplMouseEvent
- fplMouseState
- fplMutexHandle
- fplOpenGLSettings
- fplOSVersionInfos
- fplSemaphoreHandle
- fplSettings
- fplSignalHandle
- fplSpecificAudioSettings
- fplThreadHandle
- fplThreadParameters
- fplTimestamp
- fplVersionInfo
- fplVideoBackBuffer
- fplVideoRect
- fplVideoRequirements
- fplVideoRequirementsVulkan
- fplVideoSettings
- fplVideoSurface
- fplVideoSurfaceOpenGL
- fplVideoSurfaceVulkan
- fplVideoWindow
- fplVulkanSettings
- fplWindowCallbacks
- fplWindowDropFiles
- fplWindowEvent
- fplWindowPosition
- fplWindowSettings
- fplWindowSize
- fplX86CPUCapabilities