SDL School

API reference / Application

SDL_AppEvent

Function, SDL3

You write this function. SDL calls it for each event, such as a key press or a click on the close button.

Declaration

#include <SDL3/SDL.h>

SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event);

Parameters

appstateThe pointer that you stored in SDL_AppInit.
eventThe event.

Return value

SDL_APP_CONTINUE to keep running, or SDL_APP_SUCCESS or SDL_APP_FAILURE to end the program.

Example

Open a window
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
    if (event->type == SDL_EVENT_QUIT) {
        return SDL_APP_SUCCESS;
    }
    return SDL_APP_CONTINUE;
}

See this code run in Open a window. Download the full program (.zip)

In Read the keyboard

Read the keyboard
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
    if (event->type == SDL_EVENT_QUIT) {
        return SDL_APP_SUCCESS;
    }

    if (event->type == SDL_EVENT_KEY_DOWN && !event->key.repeat &&
        event->key.scancode == SDL_SCANCODE_SPACE) {
        orange = !orange;
    }
    return SDL_APP_CONTINUE;
}

See this code run in Read the keyboard. Download the full program (.zip)

Notes

In SDL2

SDL2 has no main callbacks. You call SDL_PollEvent in a loop.

See also