GLBasic forum

Main forum => GLBasic - en => Topic started by: BlueSteel on 2010-Aug-08

Title: select and key() instead of if key(xxx) ... endif
Post by: BlueSteel on 2010-Aug-08
instead of
Code (glbasic) Select

// Player Movenent
SUB scaninput:

IF KEY(52) = 1
Status$=""
GOSUB newlevel
ENDIF

IF KEY(80) = 1
Status$=""
GOSUB moveplayer_Down
ENDIF
IF KEY(79) = 1
Status$=""
GOSUB moveplayer_DownLeft
ENDIF
IF KEY(75) = 1
Status$=""
GOSUB moveplayer_Left
ENDIF
IF KEY(71) = 1
Status$=""
GOSUB moveplayer_UpLeft
ENDIF
IF KEY(72) = 1
Status$=""
GOSUB moveplayer_Up
ENDIF
IF KEY(73) = 1
Status$=""
GOSUB moveplayer_UpRight
ENDIF
IF KEY(77) = 1
Status$=""
GOSUB moveplayer_Right
ENDIF
IF KEY(81) = 1
Status$=""
GOSUB moveplayer_DownRight
ENDIF

IF KEY(16) = 1
        Status$ = "Quiting Game"
    END
    ENDIF

IF KEY(43) =1
reveal=1
ENDIF

instead want to be able to do something like this
Code (glbasic) Select

// Player Movenent
SUB scaninput:

select key()
case 52
        Status$=""
GOSUB newlevel

case 80
Status$=""
GOSUB moveplayer_Down

case 79
Status$=""
GOSUB moveplayer_DownLeft

case 75
Status$=""
GOSUB moveplayer_Left

case 71
Status$=""
GOSUB moveplayer_UpLeft

case 72
Status$=""
GOSUB moveplayer_Up

case 73
Status$=""
GOSUB moveplayer_UpRight

case 77
Status$=""
GOSUB moveplayer_Right

case 81
Status$=""
GOSUB moveplayer_DownRight

case 16
              Status$ = "Quiting Game"
              END
   
case 43
reveal=1
       desault
endselect
Title: Re: select and key() instead of if key(xxx) ... endif
Post by: MrTAToad on 2010-Aug-08
Yes, would be handy to have the scancode returned rather than checking to see if a specific scancode is pressed.

If you dont mind a slightly slower routine (and the inability to scan for a few keys), you could always use INKEY$()
Title: Re: select and key() instead of if key(xxx) ... endif
Post by: Moru on 2010-Aug-08
No, KEY() is more powerfull. Make your own routine, then you can also check for combinations of keys, not just single key presses.

FUNCTION keyCombo: keys[]
    redim keys[0]

    for n = 0 to 254
        if KEY(n) <> 0 then dimpush keys[], n
    next
ENDFUNCTION

Something like that, don't have time to test it right now :-)
Title: Re: select and key() instead of if key(xxx) ... endif
Post by: MrTAToad on 2010-Aug-08
Oh yes - KEY is more powerful, but sometimes you just want to detect a key press, not look specifically for a key.
Title: Re: select and key() instead of if key(xxx) ... endif
Post by: BlueSteel on 2010-Aug-09
the for loop isn't the solution.. it can put unnecessary load on the system..

however until / or in case we don't ever get it.. I'll have to put up with endless IF.. EndIF's..... lol

Inkey$ only reports printable characters doesn't it.. ie: no cursor keys or meta states

one solution may be pop into C++ code and then pop out again
Question .. with inline C++ code.. can you access variables used in GLasics code and visa versa..
Title: Re: select and key() instead of if key(xxx) ... endif
Post by: Slydog on 2010-Aug-09
You could try something like:

Code (glbasic) Select
FUNCTION GetKey%:
    LOCAL k% = 0
    IF KEY(52) = 1 THEN k=52
    IF KEY(79) = 1 THEN k=79
    IF KEY(80) = 1 THEN k=80
    IF KEY(82) = 1 THEN k=82
    RETURN k
ENDFUNCTION


Then you can use it in a SELECT statement like:
Code (glbasic) Select
SELECT GetKey()
CASE 52
...
CASE 80
...
ENDSELECT


This way you don't have unneeded key checks / if statements.
But, with SELECT statements you can't check for multiple checks, like is the player moving up AND shooting.

And yes, you can access variables inside INLINE and vise-versa.
Check the help file for 'INLINE' to get you started, or the forums for more specific help.
Title: Re: select and key() instead of if key(xxx) ... endif
Post by: Kitty Hello on 2010-Aug-09
Code (glbasic) Select

FUNCTION MyInkey%:
LOCAL i%
FOR i=0 TO 255; IF KEY(i) THEN RETURN i; NEXT
RETURN -1
ENDFUNCTION


I don't find that very practical, though. I'd recommend ASC(INKEY$()) instead.
Title: Re: select and key() instead of if key(xxx) ... endif
Post by: MrTAToad on 2010-Aug-09
QuoteQuestion .. with inline C++ code.. can you access variables used in GLasics code and visa versa..
Unfortunately you would need access to the keysym structure, which I dont think is possible.

It would be nice if the contents of that could be read (or DirectInput for Windows machines).
Title: Re: select and key() instead of if key(xxx) ... endif
Post by: Hemlos on 2010-Aug-11
You can try to numerically order the CASE's and it might prevent loss of key detection and possibly speed it up at the same time.

I use this to prevent missing any keys being pressed if more than one is pressed..nearly fool proof:


Code (glbasic) Select


FUNCTION KeyBoardIO:
// ------------------------------------------------------------- //
// ---  KeyBoardIO  --- detect any key press...great for user inputs
// waits till button is released...then returns that keycode.
// Doesnt do key pressed pause and repeat ...just a release return
// This will fire all key status into RETURN, one at a time from buffer..
// This allows you to do a check on all buttons pressed, not just one at a time.
// ------------------------------------------------------------- //

LOCAL KeyIndex
LOCAL IsKeyPressed = 0
LOCAL IsKeyLocked = 1
STATIC INITIALIZE

IF INITIALIZE = FALSE; INITIALIZE = TRUE;
DIM KeyBuffer[ 221 ][ 2 ]
KeyBuffer[ 0 ][ 0 ] = 0
ENDIF

//Check all key holding states:
FOR KeyIndex = 0 TO 220
IF KEY( KeyIndex )
KeyBuffer[ KeyIndex ][ IsKeyPressed ] = TRUE //Key pressed for the first time.
IF KeyBuffer[ KeyIndex ][ IsKeyLocked ] = FALSE
KeyBuffer[ KeyIndex ][ IsKeyLocked ] = TRUE
RETURN KeyIndex
ENDIF
ELSE
KeyBuffer[ KeyIndex][ IsKeyPressed ] =FALSE //Key held
KeyBuffer[ KeyIndex][ IsKeyLocked ] = FALSE
ENDIF
NEXT

RETURN -1 //Send NULL value

ENDFUNCTION