I've got a few things that are driving me nuts.
How do I clear the keyboard buffer?
Say you have a menu and you select an option or a submenu by using the ENTER key. So I press ENTER. If I come to a submenu where I can also press ENTER to select something, it is selected right away. Damn annoying. In BlitzMax I almost always use Delay 100 followed by Flushkeys after reading the keyboard (for menu related things). But none of those commands are available in GL and I can't find anything else that does the same thing.
Shouldn't the GL command KEYWAIT be called SOMEKEYSWAIT? </sarcasm>
Seriously?...why does this command not register the ESC and arrowkeys?
...and more (yeah, lots of keyboard related problems). =D
I need a 'PressAnyKey' function so I tried this;
FUNCTION PressAnyKey:
a%=0
WHILE a%=0
FOR b%=1 TO 255
IF KEY(b%) THEN a%=b%
NEXT
WEND
//I even tried adding this but I still can't make it behave the way I want it to.
b%=a%
a%=KEY(b%)
WHILE a%<>0
a%=KEY(b%)
WEND
ENDFUNCTION
But that doesn't work. All these problems are because I can't find a way to clear the keyboard buffer, and because the KEY command is so sensitive.
Gernot, I realise that you can't add all the commands from BlitzMax, because then it'd be BlitzMax :) ...but BM has the command KEYDOWN which does the same as KEY in GL, but it also has the command KEYHIT which registers ONE keyhit and doesn't continue until the key is no longer pressed. Could you perhaps add a command like that?
Can anyone help with these small things?...perhaps guide me in the right direction?
Take a look at the source for mt Glowing Rocks from Outer Space game. In the Globals.gbas file is a type definition for KEYCODES. Further down that file are two functions - KeyHitUpdate and KeyHit.
Put all of that into your project, and add: GLOBAL gKeys AS KEYCODES where you define the rest of your variables.
Then in your menu function you can use:
function mainMenu:
while (something = something else and you stay in the main menu)
KeyHitUpdate()
// rest of menu display stuff
if keyhit(28) = 2 then playGame() // Press Enter to play
showscreen
wend
endfunction
Something like that should do it. I did grab that code form a forum posting on here, but I'm not sure who wrote it originally.
QuoteHow do I clear the keyboard buffer?
You dont - you wait until the key is
released.
Yes, KEYWAIT should be able to detect the cursor and Escape keys.
BlitzMax has a keyboard buffer, GLBasic doesn't use one. Being able to detect whether a key is up or down would be handy though.
This code should help. Note that you only get notified when a key is released. I've found that this tends to solve many user selection problems instead of detecting when a mouse is pressed.
GLOBAL keys%[]; DIM keys%[256]
LOCAL result%[]
LOCAL count%
FOR count%=0 TO 255
keys%[count%]=FALSE
NEXT
WHILE TRUE
count%=keyBuffer(result%[])
IF count%=0
PRINT "no keys pressed",0,0
ELSE
PRINT "Number of keys pressed : "+count%,0,0
ENDIF
SHOWSCREEN
WEND
FUNCTION keyBuffer%:pressed%[]
LOCAL loop%
DIM pressed%[0]
FOR loop%=0 TO 255
IF KEY(loop%)
keys%[loop%]=TRUE
ELSE
IF keys%[loop%]
keys%[loop%]=FALSE
DIMPUSH pressed%[],loop%
ENDIF
ENDIF
NEXT
RETURN BOUNDS(pressed%[],0)
ENDFUNCTION
It should be in an extended type really, but I wrote it rather quickly.
Thanks for the help guys.
None of the solutions really helped me, but after hours (yes) of messing around and trying stuff out - randomly in the end - I finally managed to make it behave the way I wanted it to.
I just can't understand why a simple thing like this isn't simple to do. :|
Oh, well...it works now. That's the important bit. :)
I just add a "press" variable to any condition eg.
GLOBAL press
...
...
...
IF KEY(28) and press=0
// Do something
press=20
ENDIF
IF press>0 then DEC press
That gives you plenty of time to press the key and release before it's read again.
IF LEN(INKEY$()) might work.
I used a timer to delay about 250ms after a key press before it was recognised again, and although it worked perfectly for me, some ham-fisted people still managed to hold the key down for too long ;/
Sometimes I do:
IF KEY(a)
WHILE WEY(a); WEND
RETURN
ENDIF
- bad style though.
One other solution:
http://www.glbasic.com/forum/index.php?topic=4036.0 (can't find original as usual :) )