Author Topic: SLEEP and INKEY$ don't play well together.  (Read 2078 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
SLEEP and INKEY$ don't play well together.
« on: March 16, 2020, 07:38:20 pm »
I know SLEEP holds key presses in the buffer, but this one surprised me in that it preserved CHR$(13), even though an arrow down key was the last key pressed before the  INKEY$ loop.

Code: QB64: [Select]
  1. PRINT "Inkey$ test. Press Enter"
  2. PRINT "Now press arrow down"
  3.  
  4. blocksize = 4
  5. FOR k = 1 TO blocksize
  6.     PRINT k
  7. PRINT: PRINT "[1] Delete database entry, [0] Delete established listing, [Enter] Allow both."
  8. REM b$ = INKEY$ ' Clear buffer
  9.     _LIMIT 30
  10.     b$ = INKEY$
  11.     IF b$ = "1" OR b$ = "0" OR b$ = CHR$(13) THEN EXIT DO
  12.  
  13. IF LEN(b$) = 2 THEN
  14.     PRINT "The value of inkey$ is: "; ASC(MID$(b$, 1, 1)), ASC(MID$(b$, 2, 1))
  15.     PRINT "The value of inkey$ is: "; ASC(b$)

Of course you can remove the REM statement, so the b$ = INKEY$ can clear the buffer before the INKEY$ loop. I only posted this because I thought the SLEEP statement would return the down arrow character, the last key pressed, instead of the first key pressed,  the Enter key CHR$(13). It is probably the same in QBasic or is it not behaving correctly?

Thoughts?

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: SLEEP and INKEY$ don't play well together.
« Reply #1 on: March 16, 2020, 08:55:40 pm »
There is no clear of key buffer of keypress from SLEEP so you can create a chain of keypresses from a chain of SLEEP's.

checkout _KEYCLEAR