Saat belajar tentang Loop Utama program SDL, kita sudah mempelajari tentang cara mendapatkan
informasi terkait event yang sedang diproses dari program kita. Kita
juga sudah mempelajari cara mengetahui tombol mouse yang ditekan dengan "button". Kali ini, kita akan mempelajari cara mengetahui tombol keyboard mana yang ditekan dengan "nama_variabel.key.keysym.sym".
#include <SDL2/SDL.h>
int main(int argc, char* args[]){
SDL_Window* window = NULL;
SDL_Event evt;
SDL_Surface* screenSurface = NULL;
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ){
printf( "Error: %s\n", SDL_GetError() );
}else{
window = SDL_CreateWindow( "SDLku", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN );
if(window == NULL){
printf( "Error : %s", SDL_GetError() );
}else{
screenSurface = SDL_GetWindowSurface( window );
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF) );
while(evt.type!=SDL_QUIT){
while(SDL_PollEvent(&evt)){
if(evt.type==SDL_KEYUP){
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF) );
switch(evt.key.keysym.sym){
case SDLK_a:
SDL_FillRect(screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0x00, 0xFF, 0xFF));
break;
case SDLK_s:
SDL_FillRect(screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0x00, 0xFF));
break;
default:
SDL_FillRect(screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xF0, 0xF0, evt.key.keysym.sym*10%255));
}
}
SDL_UpdateWindowSurface( window );
}
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Pada kode program di atas, tipe event yang digunakan adalah SDL_KEYUP yang bisa juga diganti dengan SDL_KEYDOWN
jika kita ingin mendapatkan tombol saat ditekan tanpa perlu melepasnya.
Karena yang digunakan SDL_KEYUP, maka jika tombol 'a' atau 's' ditekan
lalu dilepas, seharusnya isi window akan berubah warna.
Kalian bisa menggunakan angka atau konstanta tombol yang tersedia. Kalau kalian menggunakan konstanta yang disediakan SDL, ingat bahwa semua konstanta diawali dengan SDLK_. Pada contoh di atas konstanta yang kita gunakan adalah SDLK_a dan SDLK_s. Kalian bisa mengganti a dan s dengan huruf lain jika kalian ingin mencoba tombol keyboard lain.
Beberapa konstanta di atas mungkin saja tidak akan berlaku di MAC_OS. Lebih baik kalian coba saja sendiri untuk mengecek kesesuaian tombol dengan konstantanya.
Kalian bisa menggunakan angka atau konstanta tombol yang tersedia. Kalau kalian menggunakan konstanta yang disediakan SDL, ingat bahwa semua konstanta diawali dengan SDLK_. Pada contoh di atas konstanta yang kita gunakan adalah SDLK_a dan SDLK_s. Kalian bisa mengganti a dan s dengan huruf lain jika kalian ingin mencoba tombol keyboard lain.
Nama Tombol
|
SDL_Scancode Value
|
SDL_Keycode Value
|
0
|
SDL_SCANCODE_0
|
SDLK_0
|
1
|
SDL_SCANCODE_1
|
SDLK_1
|
2
|
SDL_SCANCODE_2
|
SDLK_2
|
3
|
SDL_SCANCODE_3
|
SDLK_3
|
4
|
SDL_SCANCODE_4
|
SDLK_4
|
5
|
SDL_SCANCODE_5
|
SDLK_5
|
6
|
SDL_SCANCODE_6
|
SDLK_6
|
7
|
SDL_SCANCODE_7
|
SDLK_7
|
8
|
SDL_SCANCODE_8
|
SDLK_8
|
9
|
SDL_SCANCODE_9
|
SDLK_9
|
A
|
SDL_SCANCODE_A
|
SDLK_a
|
"Again"
|
SDL_SCANCODE_AGAIN
|
SDLK_AGAIN
|
"AltErase"
|
SDL_SCANCODE_ALTERASE
|
SDLK_ALTERASE
|
'
|
SDL_SCANCODE_APOSTROPHE
|
SDLK_QUOTE
|
"Application"
|
SDL_SCANCODE_APPLICATION
|
SDLK_APPLICATION
|
B
|
SDL_SCANCODE_B
|
SDLK_b
|
"Backspace"
|
SDL_SCANCODE_BACKSPACE
|
SDLK_BACKSPACE
|
"BrightnessDown"
|
SDL_SCANCODE_BRIGHTNESSDOWN
|
SDLK_BRIGHTNESSDOWN
|
"BrightnessUp"
|
SDL_SCANCODE_BRIGHTNESSUP
|
SDLK_BRIGHTNESSUP
|
C
|
SDL_SCANCODE_C
|
SDLK_c
|
"Calculator"
|
SDL_SCANCODE_CALCULATOR
|
SDLK_CALCULATOR
|
"Cancel"
|
SDL_SCANCODE_CANCEL
|
SDLK_CANCEL
|
"CapsLock"
|
SDL_SCANCODE_CAPSLOCK
|
SDLK_CAPSLOCK
|
"Clear"
|
SDL_SCANCODE_CLEAR
|
SDLK_CLEAR
|
"Clear / Again"
|
SDL_SCANCODE_CLEARAGAIN
|
SDLK_CLEARAGAIN
|
,
|
SDL_SCANCODE_COMMA
|
SDLK_COMMA
|
"Computer" |
SDL_SCANCODE_COMPUTER
|
SDLK_COMPUTER
|
"Copy"
|
SDL_SCANCODE_COPY
|
SDLK_COPY
|
"CrSel"
|
SDL_SCANCODE_CRSEL
|
SDLK_CRSEL
|
"CurrencySubUnit"
|
SDL_SCANCODE_CURRENCYSUBUNIT
|
SDLK_CURRENCYSUBUNIT
|
"CurrencyUnit"
|
SDL_SCANCODE_CURRENCYUNIT
|
SDLK_CURRENCYUNIT
|
"Cut"
|
SDL_SCANCODE_CUT
|
SDLK_CUT
|
D
|
SDL_SCANCODE_D
|
SDLK_d
|
"DecimalSeparator"
|
SDL_SCANCODE_DECIMALSEPARATOR
|
SDLK_DECIMALSEPARATOR
|
"Delete"
|
SDL_SCANCODE_DELETE
|
SDLK_DELETE
|
"DisplaySwitch"
|
SDL_SCANCODE_DISPLAYSWITCH
|
SDLK_DISPLAYSWITCH
|
"Down"
|
SDL_SCANCODE_DOWN
|
SDLK_DOWN
|
"E"
|
SDL_SCANCODE_E
|
SDLK_e
|
"Eject"
|
SDL_SCANCODE_EJECT
|
SDLK_EJECT
|
"End"
|
SDL_SCANCODE_END
|
SDLK_END
|
"="
|
SDL_SCANCODE_EQUALS
|
SDLK_EQUALS
|
"Esc"
|
SDL_SCANCODE_ESCAPE
|
SDLK_ESCAPE
|
"Execute"
|
SDL_SCANCODE_EXECUTE
|
SDLK_EXECUTE
|
"ExSel"
|
SDL_SCANCODE_EXSEL
|
SDLK_EXSEL
|
"F"
|
SDL_SCANCODE_F
|
SDLK_f
|
"F1"
|
SDL_SCANCODE_F1
|
SDLK_F1
|
"F10"
|
SDL_SCANCODE_F10
|
SDLK_F10
|
"F11"
|
SDL_SCANCODE_F11
|
SDLK_F11
|
"F12"
|
SDL_SCANCODE_F12
|
SDLK_F12
|
"F13"
|
SDL_SCANCODE_F13
|
SDLK_F13
|
"F14"
|
SDL_SCANCODE_F14
|
SDLK_F14
|
"F15"
|
SDL_SCANCODE_F15
|
SDLK_F15
|
"F16"
|
SDL_SCANCODE_F16
|
SDLK_F16
|
"F17"
|
SDL_SCANCODE_F17
|
SDLK_F17
|
"F18"
|
SDL_SCANCODE_F18
|
SDLK_F18
|
"F19"
|
SDL_SCANCODE_F19
|
SDLK_F19
|
"F2"
|
SDL_SCANCODE_F2
|
SDLK_F2
|
"F20"
|
SDL_SCANCODE_F20
|
SDLK_F20
|
"F21"
|
SDL_SCANCODE_F21
|
SDLK_F21
|
"F22"
|
SDL_SCANCODE_F22
|
SDLK_F22
|
"F23"
|
SDL_SCANCODE_F23
|
SDLK_F23
|
"F24"
|
SDL_SCANCODE_F24
|
SDLK_F24
|
"F3"
|
SDL_SCANCODE_F3
|
SDLK_F3
|
"F4"
|
SDL_SCANCODE_F4
|
SDLK_F4
|
"F5"
|
SDL_SCANCODE_F5
|
SDLK_F5
|
"F6"
|
SDL_SCANCODE_F6
|
SDLK_F6
|
"F7"
|
SDL_SCANCODE_F7
|
SDLK_F7
|
"F8"
|
SDL_SCANCODE_F8
|
SDLK_F8
|
"F9"
|
SDL_SCANCODE_F9
|
SDLK_F9
|
"Find"
|
SDL_SCANCODE_FIND
|
SDLK_FIND
|
"G"
|
SDL_SCANCODE_G
|
SDLK_g
|
"H"
|
SDL_SCANCODE_H
|
SDLK_h
|
"Help"
|
SDL_SCANCODE_HELP
|
SDLK_HELP
|
"Home"
|
SDL_SCANCODE_HOME
|
SDLK_HOME
|
"I"
|
SDL_SCANCODE_I
|
SDLK_i
|
"Insert"
|
SDL_SCANCODE_INSERT
|
SDLK_INSERT
|
"J"
|
SDL_SCANCODE_J
|
SDLK_j
|
"K"
|
SDL_SCANCODE_K
|
SDLK_k
|
"Keypad 0"
|
SDL_SCANCODE_KP_0
|
SDLK_KP_0
|
"Keypad 00"
|
SDL_SCANCODE_KP_00
|
SDLK_KP_00
|
"Keypad 000"
|
SDL_SCANCODE_KP_000
|
SDLK_KP_000
|
"Keypad 1"
|
SDL_SCANCODE_KP_1
|
SDLK_KP_1
|
"Keypad 2"
|
SDL_SCANCODE_KP_2
|
SDLK_KP_2
|
"Keypad 3"
|
SDL_SCANCODE_KP_3
|
SDLK_KP_3
|
"Keypad 4"
|
SDL_SCANCODE_KP_4
|
SDLK_KP_4
|
"Keypad 5"
|
SDL_SCANCODE_KP_5
|
SDLK_KP_5
|
"Keypad 6"
|
SDL_SCANCODE_KP_6
|
SDLK_KP_6
|
"Keypad 7"
|
SDL_SCANCODE_KP_7
|
SDLK_KP_7
|
"Keypad 8"
|
SDL_SCANCODE_KP_8
|
SDLK_KP_8
|
"Keypad 9"
|
SDL_SCANCODE_KP_9
|
SDLK_KP_9
|
"Keypad Enter"
|
SDL_SCANCODE_KP_ENTER
|
SDLK_KP_ENTER
|
"Keypad +/-"
|
SDL_SCANCODE_KP_PLUSMINUS
|
SDLK_KP_PLUSMINUS
|
L
|
SDL_SCANCODE_L
|
SDLK_l
|
"Left Alt"
|
SDL_SCANCODE_LALT
|
SDLK_LALT
|
"Left Ctrl"
|
SDL_SCANCODE_LCTRL
|
SDLK_LCTRL
|
"Left"
|
SDL_SCANCODE_LEFT
|
SDLK_LEFT
|
[
|
SDL_SCANCODE_LEFTBRACKET
|
SDLK_LEFTBRACKET
|
M
|
SDL_SCANCODE_M
|
SDLK_m
|
-
|
SDL_SCANCODE_MINUS
|
SDLK_MINUS
|
N
|
SDL_SCANCODE_N
|
SDLK_n
|
"Numlock"
|
SDL_SCANCODE_NUMLOCKCLEAR
|
SDLK_NUMLOCKCLEAR
|
O
|
SDL_SCANCODE_O
|
SDLK_o
|
"Oper"
|
SDL_SCANCODE_OPER
|
SDLK_OPER
|
"Out"
|
SDL_SCANCODE_OUT
|
SDLK_OUT
|
P
|
SDL_SCANCODE_P
|
SDLK_p
|
"PageDown"
|
SDL_SCANCODE_PAGEDOWN
|
SDLK_PAGEDOWN
|
"PageUp"
|
SDL_SCANCODE_PAGEUP
|
SDLK_PAGEUP
|
"Paste"
|
SDL_SCANCODE_PASTE
|
SDLK_PASTE
|
"Pause"
|
SDL_SCANCODE_PAUSE
|
SDLK_PAUSE
|
.
|
SDL_SCANCODE_PERIOD
|
SDLK_PERIOD
|
"Power"
|
SDL_SCANCODE_POWER
|
SDLK_POWER
|
"PrintScreen"
|
SDL_SCANCODE_PRINTSCREEN
|
SDLK_PRINTSCREEN
|
"Prior"
|
SDL_SCANCODE_PRIOR
|
SDLK_PRIOR
|
Q
|
SDL_SCANCODE_Q
|
SDLK_q
|
R
|
SDL_SCANCODE_R
|
SDLK_r
|
"Right Alt"
|
SDL_SCANCODE_RALT
|
SDLK_RALT
|
"Right Ctrl"
|
SDL_SCANCODE_RCTRL
|
SDLK_RCTRL
|
"Return"
|
SDL_SCANCODE_RETURN
|
SDLK_RETURN
|
"Return"
|
SDL_SCANCODE_RETURN2
|
SDLK_RETURN2
|
"Right GUI"
|
SDL_SCANCODE_RGUI
|
SDLK_RGUI
|
"Right"
|
SDL_SCANCODE_RIGHT
|
SDLK_RIGHT
|
]
|
SDL_SCANCODE_RIGHTBRACKET
|
SDLK_RIGHTBRACKET
|
"Right Shift"
|
SDL_SCANCODE_RSHIFT
|
SDLK_RSHIFT
|
S
|
SDL_SCANCODE_S
|
SDLK_s
|
"ScrollLock"
|
SDL_SCANCODE_SCROLLLOCK
|
SDLK_SCROLLLOCK
|
"Select"
|
SDL_SCANCODE_SELECT
|
SDLK_SELECT
|
;
|
SDL_SCANCODE_SEMICOLON
|
SDLK_SEMICOLON
|
"Separator"
|
SDL_SCANCODE_SEPARATOR
|
SDLK_SEPARATOR
|
/
|
SDL_SCANCODE_SLASH
|
SDLK_SLASH
|
"Spasi"
|
SDL_SCANCODE_SPACE
|
SDLK_SPACE
|
T
|
SDL_SCANCODE_T
|
SDLK_t
|
"Tab"
|
SDL_SCANCODE_TAB
|
SDLK_TAB
|
U
|
SDL_SCANCODE_U
|
SDLK_u
|
"Undo"
|
SDL_SCANCODE_UNDO
|
SDLK_UNDO
|
"Up"
|
SDL_SCANCODE_UP
|
SDLK_UP
|
V
|
SDL_SCANCODE_V
|
SDLK_v
|
"VolumeDown"
|
SDL_SCANCODE_VOLUMEDOWN
|
SDLK_VOLUMEDOWN
|
"VolumeUp"
|
SDL_SCANCODE_VOLUMEUP
|
SDLK_VOLUMEUP
|
W
|
SDL_SCANCODE_W
|
SDLK_w
|
X
|
SDL_SCANCODE_X
|
SDLK_x
|
Y
|
SDL_SCANCODE_Y
|
SDLK_y
|
Z
|
SDL_SCANCODE_Z
|
SDLK_z
|
Beberapa konstanta di atas mungkin saja tidak akan berlaku di MAC_OS. Lebih baik kalian coba saja sendiri untuk mengecek kesesuaian tombol dengan konstantanya.