Clear Keybuffer?

Previous topic - Next topic

Poetronic

Hi all,

I'm having a bit of a hard time with my hiscore function. Everything is working just fine - but one notorious problem is still waiting to be solved. The problem is that as soon as the player is asked to enter his/her name, the letters are "filled" automatically with the keys pressed withing the game loop. I use some functions in the game that need key input (such as checking the online status or showing the FPS), and I can't find a way to clear the keybuffer as soon as the EnterName() function is started. What can I do? Maybe there is a very simple solution I can't see right now...?

Best regards,
P.
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

Ian Price

Have you tried using something that delays keypresses?

Eg -
Code (glbasic) Select

FOR n=0 TO 255
IF KEY(n) AND press=0
  value=n
  press=10
ENDIF
NEXT

IF press>0 THEN DEC press



This reduces the possibility of multiple presses, by adding a delay between keypresses. You can alter the "press" value to increase/reduce the delay between keypresses.
I came. I saw. I played.

Poetronic

#2
Hi Ian,

thank you for your reply! I think that the input speed is not the problem. Instead, the problem seems to be that the input somehow remains "locked" in the keybuffer.

For example I use a key release check for the letter "o" in order to see whether or not there is an internet connection available. Then I press "f" for like 2 seconds in order to display the current FPS. The problem is that when the player has to enter a hiscore name at the end of the game, there's already something like "ooofffffff" entered as the player's name which is defined as "PLAYER_ONE" at the beginnning of the game.

The hiscore name consists of a few letters stored in an array by the way; for the input of the letters I use INKEY$().

Any ideas on how to solve this problem? I would also like to use keyboard controls for the gameplay.
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

MrTAToad

You actually need two loops : One to wait until a key is released, and then one to wait for a key to be pressed.

Ian Price

#4
The name array can easily be added to with KEY(n), if you've got an array of the key letters/value.

eg

Code (glbasic) Select

DIM kee$[255]
kee$[30]="A"
kee$[31]="S"
kee$[32]="D"
...

IF KEY(n) AND press=0
name$=name$+kee$[n]

// Or name[number]=n
// INC number
press=10

ENDIF

IF press>0 THEN DEC press


There's a million and one different methods dealing with keys and data entry.
I came. I saw. I played.

Poetronic

#5
Uhm, yes - thats exactly how I do it and it works fine :) The problem is not that the input doesn't work! The problem is that the input starts and the letters get filled in automatically with they keys pressed in the game loop.

@MrTAToad: I don't understand what you're driving at...
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

Moru

Give us some code so we can point out problems and test for ourselves. It's hard to debug. It's even harder to debug something you can't see the source of :-)

Poetronic

Hm, the code is pretty simple actually... at the core of it is the following function:

Code (glbasic) Select

key$ = UCASE$(INKEY$())

FOR i = 0 TO len_inputletter%
IF key$ = inputletter$[i]
playername$[position] = key$
INC position, 1
IF position > 9 THEN position = 0
ENDIF
NEXT


The rest deals with the positioning of the cursor and stuff like that. Problem remains: When the Function is called the letters get inserted automatically with the keys pressed in the game loop. Hope that helps?
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

MrTAToad

What you need to do is wait until a key (or keys) are released, and then you can check for keypresses.  There is no actual keyboard buffer (unless you use INPUT)

Kitty Hello

The inkey$ buffers up to 10 chars. Sp use
while len(inkey$()); wend
before you input anything.

Poetronic

Thx Kitty, works perfectly now!  :)

What about the jQuery - did it do the IE7 trick for you?
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

Kitty Hello

Had no time to try, yet. I'd rather not use jQuery.

Poetronic

Why not? Should not be a problem since you're already using Javascript... Plus it solves the problem with only a few extra lines of code. The z-Index issue is a well-known and much discussed IE bug btw :-)
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0