Page 1 of 1

Qbasic game controls

Posted: Wed Sep 29, 2004 6:59 pm
by sk8miniramps
I have made a couple tile based QBasic games that use the arrow keys to move the character. But if you hold down a key for a second and then let go the character keeps moving for another second or two. Is there a way to make him move while you hold down a key, but stop right when you let go?

Posted: Wed Sep 29, 2004 11:03 pm
by Dr. D'nar
It keeps on going because when you hold down a key, the key simply keeps sending the siginal that it's held down over and over. The BIOS on your computer has a buffer that holds recently pushed keys. What's happening is that the program is reading those buffered key strokes. Like so:
[down key]x3 -> BIOS buffer
The BIOS buffer then looks like this:
d d d
The program then reads one down key and does what it's supposed to do. The BIOS knows that one key stroke has been read and removes that key stroke from it's buffer. So the key buffer then looks like this:
d d
Then after the program is finished doing what it's doing it then reads the next stroke and so forth.

Bottem line: Make your program faster than the repete rate of the keyboard and it will work just fine. Other wise your out of luck.

Posted: Sat Oct 02, 2004 8:00 pm
by Dr_Davenstein
You can also use a library, or write your own keyboard routine. ;)

Keyboard buffer clearout

Posted: Sun Feb 11, 2007 12:42 am
by Ralph
On October of 2004, you answered a question regarding the subject matter, but didn't give the manner of solution, at this site:
http://qbasic-forum.qbcafe.net/viewtopi ... t=keyboard

I have that problem, today, and wonder if you could give me a helping hand?

Posted: Sun Feb 11, 2007 2:04 am
by Dr_Davenstein
Ralph, that link leads to this thread. Was that an accident?

Posted: Sun Feb 11, 2007 2:40 am
by Ralph
No, Dr_V. The accident was that I posted here, thinking I was sending you a PM, which I later did.

But, I believe I have solved the problem very simply, even though I'm not sure I understand the logic behind my solution. Here it is. Simply enter my "keyboard buffer emptier:

Code: Select all

FOR I = 1 TO 15
  K$ = INKEY$
NEXT I
If you can explain why it works, I would appreciate it.

EDITED SUNDAY, FEB 11, 2007 10:30 AM Central Time
I got another, better solution for the above, from red_Marvin:

Code: Select all

do while inkey$ <>"":loop
Works fine!

Posted: Tue Feb 13, 2007 3:14 am
by Ralph
I am now more knowledgeable in moving figures or sprites in QuickBASIC. I have learned that using the INKEY$() function to trap a keypress has some overhead problems, as clearing out the keyboard buffer takes a measurable time! Instead, I have learned that one can use:

Code: Select all

K = INP(96)
to capture the pressed key's scan code, and use that, instead of the value of INKEY$(). An added gain is that we do away with the slight hesitation, or jump, that we see in the sprite when we first hold down a key to move it.