-
Notifications
You must be signed in to change notification settings - Fork 74
Tutorial 03 Polling events
Oliver Simmons edited this page Feb 5, 2022
·
1 revision
There are many ways to poll input events. The blocking function SDL.waitEvent will wait indefinitely until an event happens. The non-blocking function SDL.pollEvent will iterate over all ready events.
Except if you really need synchronous scenarios, SDL.pollEvent should be used.
Download full example: 03-events.tgz
To read events, call SDL.pollEvent in a for loop. It returns a table with the information about the event. See Event for more information about the events.
while running do
-- Iterate over all events, this function does not block.
for e in SDL.pollEvent() do
if e.type == SDL.event.Quit then
running = false
elseif e.type == SDL.event.KeyDown then
print(string.format("key down: %d -> %s", e.keysym.sym, SDL.getKeyName(e.keysym.sym)))
elseif e.type == SDL.event.MouseWheel then
print(string.format("mouse wheel: %d, x=%d, y=%d", e.which, e.x, e.y))
elseif e.type == SDL.event.MouseButtonDown then
print(string.format("mouse button down: %d, x=%d, y=%d", e.button, e.x, e.y))
elseif e.type == SDL.event.MouseMotion then
print(string.format("mouse motion: x=%d, y=%d", e.x, e.y))
end
end
end
In this example we check the event's type and then log information about select event types.