QB64 Phoenix Edition
_Keyhit is devilishly evil ... here's why (warning) - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11)
+--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2)
+--- Thread: _Keyhit is devilishly evil ... here's why (warning) (/showthread.php?tid=3036)



_Keyhit is devilishly evil ... here's why (warning) - doppler - 09-15-2024

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


RE: _Keyhit is devilishly evil ... here's why (warning) - SMcNeill - 09-15-2024

_KEYCLEAR is what you're looking for.

https://qb64phoenix.com/qb64wiki/index.php/KEYCLEAR


RE: _Keyhit is devilishly evil ... here's why (warning) - TerryRitchie - 09-15-2024

You need to use _KEYCLEAR

Edit: Steve beat me to it.


RE: _Keyhit is devilishly evil ... here's why (warning) - doppler - 09-15-2024

Ok that works for me (keyclear).

You both understand why I called it devilishly evil.

Thanks