Qbasicnews.com

QBasic => QB Discussion & Programming Help => Topic started by: Ralph on February 10, 2007, 07:37:01 PM



Title: SOLVED! SEE MY REVISION. Keyboard buffer cleanout
Post by: Ralph on February 10, 2007, 07:37:01 PM
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


Title: SOLVED! SEE MY REVISION. Keyboard buffer cleanout
Post by: red_Marvin on February 11, 2007, 09:21:41 AM
I guess you could do
Code:
do : loop while inkey$ <> ""

too...


Title: SOLVED! SEE MY REVISION. Keyboard buffer cleanout
Post by: Ralph on February 11, 2007, 12:11:59 PM
Press-finger stuttered! :oops:


Title: SOLVED! SEE MY REVISION. Keyboard buffer cleanout
Post by: Ralph on February 11, 2007, 12:15:31 PM
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?


Title: SOLVED! SEE MY REVISION. Keyboard buffer cleanout
Post by: red_Marvin on February 12, 2007, 12:41:05 PM
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.


Title: SOLVED! SEE MY REVISION. Keyboard buffer cleanout
Post by: Ralph on February 12, 2007, 12:45:15 PM
Red_Marvin:

Thank you!


Title: SOLVED! SEE MY REVISION. Keyboard buffer cleanout
Post by: Antoni Gual on February 12, 2007, 04:52:35 PM
This code is not using string comparisons
Code:

do:loop while len(inkey$)

so it should be faster. Try it!


Title: SOLVED! SEE MY REVISION. Keyboard buffer cleanout
Post by: Ralph on February 12, 2007, 10:17:13 PM
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! :D