Qbasicnews.com

Full Version: SOLVED! SEE MY REVISION. Keyboard buffer cleanout
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I don't remember if I posted this question before or not, but, I'm still in the dark on this subject.

In a program I'm working on, I use a key to move a sprite to the right, and another ket to move it to the left. But, if I hold down the key, when I lift my finger, and try to go in the opposite direction, the sprit continues to go in the first direction for a while, then, finally, changes direction to the newly desired one. I understand that the keyboard buffer, which holds up to 15 keypresses, has to be cleared out to avoid this. My question, then, is, how do I clear out the keyboard buffer, if, indeed, that is what has to be done.

REVISED 7:51 pm Central Time:
I think I have found a way!!! Here it is:
Code:
'Ralph's keyboard buffer cleanout:
FOR I = 1 TO 15
  K$ = INKEY$
NEXT I
I guess you could do
Code:
do : loop while inkey$ <> ""
too...
Press-finger stuttered! :oops:
red_Marvin:

Your solution works perfectly! I have changed my code accordingly. Thank you for your contribution!

Question. Why do the two methods, FOR/NEXT and DO/LOOP clear the keyboard memory buffer? Does the mere mention of INKEY$ extract the stored data, one by one, until it is empty?
Yes (IIRC) when calling INKEY$ in any way it will take the first (if any) character (or characters for special keys) and remove it from the buffer and return it.
Red_Marvin:

Thank you!
This code is not using string comparisons
Code:
do:loop while len(inkey$)
so it should be faster. Try it!
Antoni:

Thanks for your post. I tried your suggestion, as well as Red_Marvin's, but couldn't see any difference in speed in the sprites I was trying to move.

By experimenting with something suggested, I don't remember where, and using the key scan codes, I was able to really speed things up. I used:
Code:
K = INP(96)

I now believe that using INKEY$ and having to clean out the keyboard buffer truly slows things down, and that using the INP() function to get the scan codes, which are not saved in a buffer, really speeds things up many, many times. In fact, I now had to put in a time delay, to avoid moving my figures almost instantaneously.

Another perk that I got was the elimination of the slight hesitation in movement for the first keyboard entry that I was getting with the INKEY$() function. It's now gone! Big Grin