Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
_Keyhit is devilishly evil ... here's why (warning)
#1
Tried doing a little program.  Wanted to use the cursor keys as movement (left, right, up and down) followed by enter when done.  _Inkey$ wasn't going to work for me.
Came upon _Keyhit.  A value is returned as key codes.  Nice. Until I used it.

Grab a key code input: (Q would abort the program)
        Do
            _Limit 5
            x = _KeyHit
            If x = 81 Or x = 113 Then End
            If x < 0 Then x = 0
            If x <> 0 Then Exit Do
        Loop

' do some wild and interesting things here based upon cursor keys, unimportant to the my problem.

' exiting here after the magic

' the main block code is executed twice once as a test run then the real thing

If testrun = 0 Then End

*** insert below code here to fix it ***

Input "hit enter to start the real run or anything to quit"; a$
If a$ = "" Then testrun = 0: GoTo reruntop

The above line is the last line of code.  Which would default "end" if anything is input except just enter.
The devilishly evil thing about _Keyhit, which I assumed and didn't realize.  It's nothing like Inkey$
_Keyhit returns key codes, but does not modify the keyboard buffer when a key code is observed.
aka: Schrödinger's cat is still in the box re:keyboard buffer.  Wither you looked or not.

Happy ending.  Do this before next input requirement:

Do
    a$ = InKey$
Loop Until a$ = ""

Happy trails. Big Grin
Reply
#2
_KEYCLEAR is what you're looking for.

https://qb64phoenix.com/qb64wiki/index.php/KEYCLEAR
Reply
#3
You need to use _KEYCLEAR

Edit: Steve beat me to it.
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply
#4
Ok that works for me (keyclear).

You both understand why I called it devilishly evil.

Thanks
Reply




Users browsing this thread: 5 Guest(s)