Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sdlBasic library to poll input devices and generate discrete events like key_down, key_up & co.
' events.sdlbas: sdlBasic library to poll input devices
' and generate discrete events like key_down, key_up & co.
' 2019-05-07 No Time To Play <https://notimetoplay.org/>
' Use as you like; please keep the attribution if you will.
const no_event = 0
const key_down_event = 1
const key_up_event = 2
const mouse_down_event = 3
const mouse_up_event = 4
const joy_hat_up = 1
const joy_hat_right = 2
const joy_hat_down = 4
const joy_hat_left = 8
dim common event_wait_interval = 5
dim common mouse_event_x = -1
dim common mouse_event_y = -1
dim common mouse_event_button = 0
dim common key_event_code = -1
const num_event_keys = 60
dim common _event_key_codes[1 to num_event_keys]
dim common _event_key_state[1 to num_event_keys]
_event_key_codes[1] = k_1
_event_key_codes[2] = k_2
_event_key_codes[3] = k_3
_event_key_codes[4] = k_4
_event_key_codes[5] = k_5
_event_key_codes[6] = k_6
_event_key_codes[7] = k_7
_event_key_codes[8] = k_8
_event_key_codes[9] = k_9
_event_key_codes[10] = k_0
_event_key_codes[11] = k_f1
_event_key_codes[12] = k_f2
_event_key_codes[13] = k_f3
_event_key_codes[14] = k_f4
_event_key_codes[15] = k_f5
_event_key_codes[16] = k_f6
_event_key_codes[17] = k_f7
_event_key_codes[18] = k_f8
_event_key_codes[19] = k_f9
_event_key_codes[20] = k_f10
_event_key_codes[21] = k_f11
_event_key_codes[22] = k_f12
_event_key_codes[23] = k_f13
_event_key_codes[24] = k_f14
_event_key_codes[25] = k_f15
_event_key_codes[26] = k_esc
_event_key_codes[27] = k_space
_event_key_codes[28] = k_return
_event_key_codes[29] = k_tab
_event_key_codes[30] = k_backspace
_event_key_codes[31] = k_up
_event_key_codes[32] = k_down
_event_key_codes[33] = k_right
_event_key_codes[34] = k_left
_event_key_codes[35] = k_delete
_event_key_codes[36] = k_insert
_event_key_codes[37] = k_home
_event_key_codes[38] = k_end
_event_key_codes[39] = k_pageup
_event_key_codes[40] = k_pagedown
_event_key_codes[41] = k_minus
_event_key_codes[42] = k_plus
_event_key_codes[43] = k_comma
_event_key_codes[44] = k_period
_event_key_codes[45] = k_question
_event_key_codes[46] = k_kp0
_event_key_codes[47] = k_kp1
_event_key_codes[48] = k_kp2
_event_key_codes[49] = k_kp3
_event_key_codes[50] = k_kp4
_event_key_codes[51] = k_kp5
_event_key_codes[52] = k_kp6
_event_key_codes[53] = k_kp7
_event_key_codes[54] = k_kp8
_event_key_codes[55] = k_kp9
_event_key_codes[56] = k_kp_period
_event_key_codes[57] = k_kp_minus
_event_key_codes[58] = k_kp_plus
_event_key_codes[59] = k_kp_enter
_event_key_codes[60] = k_backquote
function poll_keyboard()
dim i, k
for i = 1 to num_event_keys
k = _event_key_codes[i]
if key(k) then
if not _event_key_state[i] then
_event_key_state[i] = true
key_event_code = k
return key_down_event
end if
else
if _event_key_state[i] then
_event_key_state[i] = false
key_event_code = k
return key_up_event
end if
end if
next
key_event_code = -1
return no_event
end function
' Because the mousebutton built-in only returns the last button pressed,
' this function can't properly keep track of which button was released.
function poll_mouse()
dim b = mousebutton
if b > 0 then
if mouse_event_button = b then
return no_event
else
mouse_event_button = b
mouse_event_x = mousex
mouse_event_y = mousey
return mouse_down_event
end if
else
if mouse_event_button > 0 then
mouse_event_button = 0
mouse_event_x = mousex
mouse_event_y = mousey
return mouse_up_event
else
return no_event
end if
end if
end function
function poll_event()
dim e = poll_keyboard
if e <> no_event then
return e
else
return poll_mouse
end if
end function
function wait_for_event()
dim e
do
e = poll_event
if e <> no_event then
exit do ' Don't return here directly or it freezes!
else
wait(event_wait_interval)
end if
loop
return e
end function
sub hat_to_xy(value, x, y)
if andbit(value, joy_hat_left) then
x = -1
elseif andbit(value, joy_hat_right) then
x = 1
else
x = 0
end if
if andbit(value, joy_hat_up) then
y = -1
elseif andbit(value, joy_hat_down) then
y = 1
else
y = 0
end if
end sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.