SDL_GetKeyboardState(3)

πŸ’– SDL_GetKeyboardState πŸ’–

okay bestie, so this is a page from the SDL3 FUNCTIONS manual, and it's genuinely one of the most useful little gremlins in the whole keyboard family. let's get into it πŸŽ€


✨ NAME ✨

SDL_GetKeyboardState β€” grab a snapshot of the current state of the keyboard πŸ“ΈβŒ¨οΈ


✨ SYNOPSIS ✨

#include <SDL3/SDL_keyboard.h>

const bool * SDL_GetKeyboardState(int *numkeys);

✨ DESCRIPTION ✨

so the pointer you get back? it's a pointer to an internal SDL array. like, SDL's own array. it stays valid for the whole lifetime of the application, and you should not free it yourself β€” not your array, not your problem, hands off πŸ’…

if an element in that array is true, that key is pressed. if it's false, it is not. periodt. and the way you index into it is by using SDL_Scancode values. iykyk πŸ”‘

want the state array to actually update? call SDL_PumpEvents(). that's the vibe check that refreshes everything 🫢

this function hands you the current state after all events have been processed. which means β€” and this is the important part, don't skip it β€” if a key or button got pressed and released before you ever processed events, then that pressed state will never show up in your SDL_GetKeyboardState() calls. it's gone. poof. not coming back 😭

Note: this function doesn't take into account whether shift has been pressed or not. like, shift is simply not part of the conversation here πŸ™ˆ

✨ FUNCTION PARAMETERS ✨

  • numkeys β€” if it's non-NULL, it receives the length of the returned array. if it's NULL, she just doesn't tell you the length, no biggie 🌸

✨ RETURN VALUE ✨

Returns a pointer to an array of key states. that's the whole gift 🎁


✨ THREAD SAFETY ✨

it is safe to call this function from any thread. yes bestie, any. go off πŸ‘‘


✨ AVAILABILITY ✨

this function is available since SDL 3.2.0 ⭐


✨ SEE ALSO ✨


Simple Directmedia Layer β€’ SDL 3.4.18 β€’ SDL_GetKeyboardState(3)

and that's the whole page, bestie πŸ¦‹ mwah, go press some keys β€” this doc is from Simple Directmedia Layer, SDL 3.4.18, and it's literally titled SDL_GetKeyboardState(3). slay πŸ’–βœ¨

πŸ“¦ where this page comes from