Posted: 15th Sep 2011 20:15
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

+ Code Snippet
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----------------------------------------------------
+ Code Snippet
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 !

+ Code Snippet
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!
+ Code Snippet
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

Posted: 15th Sep 2011 20:31
case select is faster then all those if statements
Posted: 15th Sep 2011 20:35
case select is faster then all those if statements

I wanted improvements so show me wath you mean

I didt optimize the code as it works very fast as it is!

How do you do an case select that checks a string and a value fast ?

Case select would simply make you go back to various number checks and dont make it simple to make keypress checks inside your code?

I want it simple like.
if KeyBoard( "S" ) = 1 then SaveLevel()

and not!
if GetRawKeyPressed( 83 ) then SaveLevel()

Its easy to forget wath letter you assigned to that event then.
Posted: 15th Sep 2011 22:59
Pretty much all of that code is redundant.

Just create constants and check them when neeeded.

#constant myKey_A = 65

If GetRawKeyPressed(myKey_A) Then ...

Nice and simple.
Posted: 15th Sep 2011 23:05
f GetRawKeyPressed(myKey_A) Then ...


So is it better to have that for all the letters and numbers on the keyboard instead of this line that to do all the neaded checks for both letters and numbers?

this checks a-z and 0-9
if key = Chr( Pressed_Key ) then IsTrue = 1

Yes your sample works if you only use a very few keys!

I can only see that your code would become large if you use an large amount of keyboard keys in your app?

My function that only checks for a-z an 0-9 will be this small!

+ Code Snippet
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



Posted: 16th Sep 2011 0:40
Yeah, just create a new file, chuck all your constants in a function, and forget about it.

Every key is covered and you don't need to start adding bits every time you want to check a key you haven't previously allowed for.

I haven't tested your code, but does it support multiple keypresses? Can you (for instance) press up and left at the same time and get the expected result?
Posted: 16th Sep 2011 7:30
I haven't tested your code, but does it support multiple keypresses? Can you (for instance) press up and left at the same time and get the expected result?

it neads to be rewritten then as get raw last key only does wath it says.

Sounds like you are talking about movement controls that aint neaded as we have a bunch of them that are simple enough already inside agk.

Yeah, just create a new file, chuck all your constants in a function, and forget about it.

Also one way that would work!
I whas doing something similar wen i started reading about the line of code bugg in agk.
Posted: 17th Sep 2011 2:27
I updated the first post as i encountered the darn string bugg anyway

Got back to using constants



+ Code Snippet
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


Thanks for the heads up bursar!

This looks alot cleaner to
Posted: 17th Sep 2011 11:25
Thanks for the heads up bursar!


I knew you'd see it my way in the end
Posted: 17th Sep 2011 11:34
I knew you'd see it my way in the end

It whas the only way to get rid of the weird bugg that crashes agk apps

constants seams to use alot less memory and use less lines of code in bytecode?

I just hope i dont hit a ceiling with constants also

cheers

Ps....
Next time so could i at least extract the editor out of the game to get some more space for ingame code
Posted: 17th Sep 2011 19:26
This is nice to have, but I don't understand the use of this
+ Code Snippet
function KeyBoard( key )
    IsTrue = GetRawKeyPressed( key )
endfunction IsTrue

function.
can't you just use
+ Code Snippet
GetRawKeyPressed( key )

?

Edit: Actually, this doesn't seem to work for me, any of it. The DBPro keystate checker returns different values for the keys than what you use, but I assume they opperate differently. Either way nothing I do seems to work.
Posted: 17th Sep 2011 22:12
The DBPro keystate checker returns different values

The code is for AppGameKit, and it returns different key values than DBP.
Posted: 17th Sep 2011 23:44
Oh, yes, I'm aware. I AM using AppGameKit I tried using the DBP keystate values just to check if they worked. I meant that neither works, more importantly the ones provided I receive no responce from the keys once-o-ever

Edit: Nevermind, it was the print() command acting up. I still don't know what exactly was wrong with it, but it doesn't matter, good job!
Posted: 18th Sep 2011 10:57


And sure you can skip the entire keyboard function!

Iam only trying to get around using the command to many times inside my code because of the bytecode bugg.

You nead an value that the function can return

dit: Nevermind, it was the print() command acting up. I still don't know what exactly was wrong with it, but it doesn't matter, good job!


You first have to activate the short cut commands with the gosub in the start of your app outside the main loop.

Always good that someone finds it usefull

If anyone have improvements so please add them so that we can get an complete key list

Iam struggeling with my tile map editor and the function that draws it for mobile apps with very little memory.

Iam bumping my head against the weird bytecode size limit