Qbasicnews.com
November 17, 2019, 04:08:51 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: SOLVED! SEE MY REVISION. Keyboard buffer cleanout  (Read 3254 times)
Ralph
Ancient Guru
****
Posts: 544


« 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
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
red_Marvin
Na_th_an
*****
Posts: 1509



WWW
« Reply #1 on: February 11, 2007, 09:21:41 AM »

I guess you could do
Code:
do : loop while inkey$ <> ""

too...
Logged

/post]
Ralph
Ancient Guru
****
Posts: 544


« Reply #2 on: February 11, 2007, 12:11:59 PM »

Press-finger stuttered! :oops:
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Ralph
Ancient Guru
****
Posts: 544


« Reply #3 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?
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
red_Marvin
Na_th_an
*****
Posts: 1509



WWW
« Reply #4 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.
Logged

/post]
Ralph
Ancient Guru
****
Posts: 544


« Reply #5 on: February 12, 2007, 12:45:15 PM »

Red_Marvin:

Thank you!
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Antoni Gual
Na_th_an
*****
Posts: 1434



WWW
« Reply #6 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!
Logged

Antoni
Ralph
Ancient Guru
****
Posts: 544


« Reply #7 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! Cheesy
Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!