SDL, keyboard and event handling
- Posted by ChrisB (moderator) Nov 01, 2022
- 1029 views
Hi
Using Andy's SDL2 lib
Pointers drive me insane!
to check the keyboard to see if ESC is pressed
atom key = 0 integer scanning = 1, scancode while scanning = 1 do SDL_PumpEvents() key = SDL_GetKeyboardState(key) if peek(key+SDL_SCANCODE_ESCAPE) > 0 then scanning = 0 end if end while return scancode
to check the keyboard for any key pressed
atom key = 0, ev_type = 0 integer scanning = 1, scancode while scanning = 1 do SDL_PumpEvents() key = SDL_GetKeyboardState(key) --if peek(key+SDL_SCANCODE_ESCAPE) > 0 then -- scanning = 0 --end if for i = 4 to 99 do if peek(key + i) > 0 then scanning = 0 scancode = i end if end for end while return scancode
These work, and do the job.
But, to check the keyboard for an event, like keydown, or indeed other event types
atom ev = 0, ev_type = 0 printf(1, "M_wait_key\n", {}) while SDL_WaitEvent(ev) do if ev > 0 then ev_type = peek(ev) printf(1, "%d, %d\n", {ev, ev_type}) if wait_key() then end if if ev_type = SDL_KEYDOWN then return 1 end if end if end while return 0 while 1 do if SDL_PollEvent(ev) then if ev > 0 then ev_type = peek(ev) printf(1, "%d, %d\n", {ev, ev_type}) if ev_type = SDL_KEYDOWN then return 1 end if end if end if end while return 0
Neither of these work
My thinking went like this
--Uses pointers and members addressing --SDL_PollEvent to get the next event from the event queue --to put in a test_event structure, --the first member of which will be the event type --all events have different structures (with members specific to the structure) --but for this function only need the first member. --see SDL_KEYBOARD event --Uint32 type the event type; SDL_KEYDOWN or SDL_KEYUP
Any pointers (sorry) in the right direction would be greatly appreciated.
Cheers Chris
Update : just remembering this has kind of already been addressed, but reading the docs, pollevent places the next event structure from the event queue into an event structure (which doesn't look like a union), so we should be able to get the event type, the first member, from that structure.
Cheers
Chris