GLBasic forum

Main forum => GLBasic - en => Topic started by: Poetronic on 2012-Sep-26

Title: Clear Keybuffer?
Post by: Poetronic on 2012-Sep-26
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.
Title: Re: Clear Keybuffer?
Post by: Ian Price on 2012-Sep-26
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.
Title: Re: Clear Keybuffer?
Post by: Poetronic on 2012-Sep-26
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.
Title: Re: Clear Keybuffer?
Post by: MrTAToad on 2012-Sep-26
You actually need two loops : One to wait until a key is released, and then one to wait for a key to be pressed.
Title: Re: Clear Keybuffer?
Post by: Ian Price on 2012-Sep-26
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.
Title: Re: Clear Keybuffer?
Post by: Poetronic on 2012-Sep-26
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...
Title: Re: Clear Keybuffer?
Post by: Moru on 2012-Sep-26
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 :-)
Title: Re: Clear Keybuffer?
Post by: Poetronic on 2012-Sep-26
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?
Title: Re: Clear Keybuffer?
Post by: MrTAToad on 2012-Sep-26
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)
Title: Clear Keybuffer?
Post by: Kitty Hello on 2012-Sep-27
The inkey$ buffers up to 10 chars. Sp use
while len(inkey$()); wend
before you input anything.
Title: Re: Clear Keybuffer?
Post by: Poetronic on 2012-Sep-27
Thx Kitty, works perfectly now!  :)

What about the jQuery - did it do the IE7 trick for you?
Title: Clear Keybuffer?
Post by: Kitty Hello on 2012-Sep-29
Had no time to try, yet. I'd rather not use jQuery.
Title: Re: Clear Keybuffer?
Post by: Poetronic on 2012-Sep-29
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 :-)