QB64 inkey$ requires multiple presses

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Albert
Coder
Posts: 14
Joined: Mon Jul 04, 2016 10:30 am

QB64 inkey$ requires multiple presses

Post by Albert »

so QB64 inkey$ requires multiple presses to register a key press
e.g. :

DO
IF inkey$ = "a" THEN GOTO 100
IF inkey$ = "s" THEN GOTO 200
IF inkey$ = "d" THEN GOTO 300
LOOP UNTIL inkey$ = "a" OR inkey$ = "s" OR inkey$ = "d"

100 PRINT "You pressed A."
200 PRINT "You pressed S."
300 PRINT "You pressed D."

how do I counter this bug?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: QB64 inkey$ requires multiple presses

Post by burger2227 »

Not a bug! You were reading the INKEY$ function many times and results were read and changed each time:

Code: Select all

DO 
K$ = LCASE$(INKEY$) ' K holds first Inkey$ result and lowers uppercase types too
LOOP UNTIL K$ = "a" OR K$ = "s" OR K$ = "d" 

IF K$ = "a" THEN PRINT "You pressed A."
IF K$ = "s" THEN PRINT "You pressed S."
IF K$ = "d" THEN PRINT "You pressed D." 
Since case is lower you do not need to check for both "d" and "D"
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Albert
Coder
Posts: 14
Joined: Mon Jul 04, 2016 10:30 am

Re: QB64 inkey$ requires multiple presses

Post by Albert »

burger2227 wrote:Not a bug! You were reading the INKEY$ function many times and results were read and changed each time:

Code: Select all

DO 
K$ = LCASE$(INKEY$) ' K holds first Inkey$ result and lowers uppercase types too
LOOP UNTIL K$ = "a" OR K$ = "s" OR K$ = "d" 

IF K$ = "a" THEN PRINT "You pressed A."
IF K$ = "s" THEN PRINT "You pressed S."
IF K$ = "d" THEN PRINT "You pressed D." 
Since case is lower you do not need to check for both "d" and "D"
thank you really much! :D
I would claim this best answer if I could :P
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: QB64 inkey$ requires multiple presses

Post by burger2227 »

That is why we are here !
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply