Started on my level editor yesterday and got irritated on the bad key press commands.
New and alot better function!
Thanks to bursar on the heads up
This seams to not cause the bad string bugg at least that i encountered today
Use it like this!
if KeyBoard( KB_esc )=1 then end
Initiate_Keyboard:
#constant KB_esc 27
#constant KB_tab 9
#constant KB_back 8
#constant KB_space 32
#constant KB_enter 13
#constant KB_shift 16
#constant KB_ctrl 17
#constant KB_up 38
#constant KB_down 40
#constant KB_left 37
#constant KB_right 39
#constant KB_0 48
#constant KB_1 49
#constant KB_2 50
#constant KB_3 51
#constant KB_4 52
#constant KB_5 53
#constant KB_6 54
#constant KB_7 55
#constant KB_8 56
#constant KB_9 57
#constant KB_np_0 45
#constant KB_np_1 35
#constant KB_np_2 40
#constant KB_np_3 34
#constant KB_np_4 37
#constant KB_np_5 12
#constant KB_np_6 39
#constant KB_np_7 36
#constant KB_np_8 38
#constant KB_np_9 33
#constant KB_a 65
#constant KB_b 66
#constant KB_c 67
#constant KB_d 68
#constant KB_e 69
#constant KB_f 70
#constant KB_g 71
#constant KB_h 72
#constant KB_i 73
#constant KB_j 74
#constant KB_k 75
#constant KB_l 76
#constant KB_m 77
#constant KB_n 78
#constant KB_o 79
#constant KB_p 80
#constant KB_q 81
#constant KB_r 82
#constant KB_s 83
#constant KB_t 84
#constant KB_u 85
#constant KB_v 86
#constant KB_w 87
#constant KB_x 88
#constant KB_y 89
#constant KB_z 90
return
function KeyBoard( key )
IsTrue = GetRawKeyPressed( key )
endfunction IsTrue
OLD----------------------------------------------------
function KeyBoard( key as string )
IsTrue = 0
// Returns 1 if the current device has a full sized keyboard,
// 2 if the device has a virtual or mobile phone keyboard and 0 for no keyboard at all.
// we only want this to work on an pc or mac so if no full keyboard jump to end
if GetKeyboardExists()<>1 then goto exit_keyboard
// if no key pressed jump to end
if GetRawKeyPressed( GetRawLastKey( ) ) = 0 then goto exit_keyboard
Pressed_Key = GetRawLastKey( )
// all the key press checks
// system
if key = "esc" and Pressed_Key = 27 then IsTrue = 1
if key = "tab" and Pressed_Key = 9 then IsTrue = 1
if key = "back" and Pressed_Key = 8 then IsTrue = 1
if key = "space" and Pressed_Key = 32 then IsTrue = 1
if key = "enter" and Pressed_Key = 13 then IsTrue = 1
if key = "shift" and Pressed_Key = 16 then IsTrue = 1
if key = "ctrl" and Pressed_Key = 17 then IsTrue = 1
// arrow
if key = "up" and Pressed_Key = 38 then IsTrue = 1
if key = "down" and Pressed_Key = 40 then IsTrue = 1
if key = "left" and Pressed_Key = 37 then IsTrue = 1
if key = "right" and Pressed_Key = 39 then IsTrue = 1
// alphabet ( only checks Large letters and numbers )
if key = Chr( Pressed_Key ) then IsTrue = 1
exit_keyboard:
endfunction IsTrue
My function that only checks for a-z an 0-9 !
function KeyBoard( key as string )
IsTrue = 0
if GetKeyboardExists()<>1 then goto exit_keyboard
if GetRawKeyPressed( GetRawLastKey( ) ) = 0 then goto exit_keyboard
if key = Chr( GetRawLastKey( )) then IsTrue = 1
exit_keyboard:
endfunction IsTrue
If anyone have better ideas to do this ?
Please improve my code
And to the question on how to use it ?
Simply do this!
// S key saves current level
if KeyBoard( "S" ) = 1 then SaveLevel()
// escape key exits game
if KeyBoard( "esc" ) = 1 then end
edited-------
I forgot to mention that is detects if s is pressed and not S ,you dont nead to press caps lock or anything to use them in you app.
the Chr() command prints out large letters thats why i use them.
-------------
Hope anyone finds it usefull?
I simply encountered the weird command limit and string limit where this helped alot.
My first function looked like this that made the agk crash and burn!
function KeyBoard( key as string )
IsTrue = 0
// Returns 1 if the current device has a full sized keyboard,
// 2 if the device has a virtual or mobile phone keyboard and 0 for no keyboard at all.
// we only want this to work on an pc or mac so if no full keyboard jump to end
if GetKeyboardExists()<>1 then goto exit_keyboard
// if no key pressed jump to end
if GetRawKeyPressed( GetRawLastKey( ) ) = 0 then goto exit_keyboard
Pressed_Key = GetRawLastKey( )
// all the key press checks
// system
if key = "esc" and Pressed_Key = 27 then IsTrue = 1
if key = "tab" and Pressed_Key = 9 then IsTrue = 1
if key = "back" and Pressed_Key = 8 then IsTrue = 1
if key = "space" and Pressed_Key = 32 then IsTrue = 1
if key = "enter" and Pressed_Key = 13 then IsTrue = 1
if key = "shift" and Pressed_Key = 16 then IsTrue = 1
if key = "ctrl" and Pressed_Key = 17 then IsTrue = 1
// arrow
if key = "up" and Pressed_Key = 38 then IsTrue = 1
if key = "down" and Pressed_Key = 40 then IsTrue = 1
if key = "left" and Pressed_Key = 37 then IsTrue = 1
if key = "right" and Pressed_Key = 39 then IsTrue = 1
// numbers
if key = "0" and Pressed_Key = 48 then IsTrue2 = 1
if key = "1" and Pressed_Key = 49 then IsTrue2 = 1
if key = "2" and Pressed_Key = 50 then IsTrue2 = 1
if key = "3" and Pressed_Key = 51 then IsTrue2 = 1
if key = "4" and Pressed_Key = 52 then IsTrue2 = 1
if key = "5" and Pressed_Key = 53 then IsTrue2 = 1
if key = "6" and Pressed_Key = 54 then IsTrue2 = 1
if key = "7" and Pressed_Key = 55 then IsTrue2 = 1
if key = "8" and Pressed_Key = 56 then IsTrue2 = 1
if key = "9" and Pressed_Key = 57 then IsTrue2 = 1
// alphabet
if key = "a" and Pressed_Key = 65 then IsTrue = 1
if key = "b" and Pressed_Key = 66 then IsTrue = 1
if key = "c" and Pressed_Key = 67 then IsTrue = 1
if key = "d" and Pressed_Key = 68 then IsTrue = 1
if key = "e" and Pressed_Key = 69 then IsTrue = 1
if key = "f" and Pressed_Key = 70 then IsTrue = 1
if key = "g" and Pressed_Key = 71 then IsTrue = 1
if key = "h" and Pressed_Key = 72 then IsTrue = 1
if key = "i" and Pressed_Key = 73 then IsTrue = 1
if key = "j" and Pressed_Key = 74 then IsTrue = 1
if key = "k" and Pressed_Key = 75 then IsTrue = 1
if key = "l" and Pressed_Key = 76 then IsTrue = 1
if key = "m" and Pressed_Key = 77 then IsTrue = 1
if key = "n" and Pressed_Key = 78 then IsTrue = 1
if key = "o" and Pressed_Key = 79 then IsTrue = 1
if key = "p" and Pressed_Key = 80 then IsTrue = 1
if key = "q" and Pressed_Key = 81 then IsTrue = 1
if key = "r" and Pressed_Key = 82 then IsTrue = 1
if key = "s" and Pressed_Key = 83 then IsTrue = 1
if key = "t" and Pressed_Key = 84 then IsTrue = 1
if key = "u" and Pressed_Key = 85 then IsTrue = 1
if key = "v" and Pressed_Key = 86 then IsTrue = 1
if key = "w" and Pressed_Key = 87 then IsTrue = 1
if key = "x" and Pressed_Key = 88 then IsTrue = 1
if key = "y" and Pressed_Key = 89 then IsTrue = 1
if key = "z" and Pressed_Key = 90 then IsTrue = 1
// numpad
// numbers
if key = "np_0" and Pressed_Key = 45 then IsTrue = 1
if key = "np_1" and Pressed_Key = 35 then IsTrue = 1
if key = "np_2" and Pressed_Key = 40 then IsTrue = 1
if key = "np_3" and Pressed_Key = 34 then IsTrue = 1
if key = "np_4" and Pressed_Key = 37 then IsTrue = 1
if key = "np_5" and Pressed_Key = 12 then IsTrue = 1
if key = "np_6" and Pressed_Key = 39 then IsTrue = 1
if key = "np_7" and Pressed_Key = 36 then IsTrue = 1
if key = "np_8" and Pressed_Key = 38 then IsTrue = 1
if key = "np_9" and Pressed_Key = 33 then IsTrue = 1
exit_keyboard:
endfunction IsTrue