Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SOLVED! SEE MY REVISION. Keyboard buffer cleanout
#1
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
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Reply
#2
I guess you could do
Code:
do : loop while inkey$ <> ""
too...
/post]
Reply
#3
Press-finger stuttered! :oops:
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Reply
#4
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?
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Reply
#5
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.
/post]
Reply
#6
Red_Marvin:

Thank you!
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Reply
#7
This code is not using string comparisons
Code:
do:loop while len(inkey$)
so it should be faster. Try it!
Antoni
Reply
#8
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
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)