DAY 006: _KEYCLEAR
#1
This is, in my opinion, one of those essential keywords that everyone should know and make use of.

What is it?  Keyclear is the simple command which clears the keyboard buffers for us.

How does one use it?  Just call _KEYCLEAR where your program would need to clear the buffers at.

Example of the command:

Code: (Select All)
Print "Kindly play around with your keyboard some, and just press a few random keys for the next few seconds."
_Delay 5
Print "The keys you pressed were:"
Do
    k = _KeyHit
    If k > 0 Then i$ = InKey$: Print k, i$
Loop Until k = 0

Print
Print
Print "Well now, isn't that something?  Your keystrokes were recorded and reported several seconds AFTER you typed them into the keyboard, and affected your program even after your _DELAY!"
Print
Print "So how to prevent that type of issue?"
Print
Print "Captain _KEYCLEAR to the rescue!!!"
Print
Print "Press <ANY KEY> to see the solution in action."
Sleep
_KeyClear
Cls
Print "Kindly play around with your keyboard some, and just press a few random keys for the next few seconds."
_Delay 5
Print "The keys you pressed were:"
_KeyClear: Print "<<Captian _KEYCLEAR to the rescue here!!!>>"
Do
    k = _KeyHit
    If k > 0 Then i$ = InKey$: Print k, i$
Loop Until k = 0


As you can see from the simple example above, _DELAY doesn't stop our program from recording keypresses and adding them into our internal keyboard buffers.   (Neither does SLEEP, or several other "pause" type commands.)  Even though our program appears inactive, it's actually running the keyboard handlers in the background and collecting our last several keystrokes and saving those in a buffer for us.

How could that be bad for us?

Imagine you have a game which pauses execution with a series of set _DELAYs so it can shell out to a video player to play a cutscene for that game.  The user doesn't want to watch the cutscene, so they hit ESC, CTRL-F4, ALT-F4, and multiple other things to try and close the video early, only to find out the video doesn't support those keystrokes...  They watch the cutscene.  Control moves past the delays and back to your program.. Which now tries to process all those keystrokes in the buffer and...  BAM!!  Game closes without saving!!

AAARRRGGHHH!!!   (Who else just threw their controller through the monitor??  Fess up and admit it!  We've all been there. Tongue )

So, what's the easiest way to fix that type of issue?

Simply _KEYCLEAR after playing your cutscene and be done with it.  

Lots of programs have issues by reading unintended keystrokes from the buffers after a pause or delay.  The easiest way to fix those issues is with _KEYCLEAR.  It's your friend -- get to know him well.  Wink
Reply
#2
I love _KEYCLEAR. Anyone who doesn't needs to be put in a paper bag... Well, you all know the drill!

In the QJurASCIIc Period, we had to code ways to clear the key buffer like: WHILE LEN(INKEY$): WEND

So many uses...

SLEEP will store a keystroke. Using _KEYCLEAR after it will get rid of it.

If a user holds down keys, but only one unique response needs to be registered, using _KEYCLEAR clears the rest of the duplicate entries out of the buffer.

Can be used to control the repeat rate as follows:

Code: (Select All)
DO
    _LIMIT 120
    b$ = INKEY$
    IF LEN(b$) THEN
        PRINT b$;
        _DELAY .1
        _KEYCLEAR ' Alternative 'WHILE LEN(INKEY$): WEND
    END IF
LOOP

Anyway, very neat and handy tool for us SCREEN ZERO Heroes. No "Fire up the bag!" rating, here.

Thanks Magic 8-Ball,

Pete
Reply
#3
(11-11-2022, 04:33 PM)SMcNeill Wrote: This is, in my opinion, one of those essential keywords that everyone should know and make use of.

What is it?  Keyclear is the simple command which clears the keyboard buffers for us.

How does one use it?  Just call _KEYCLEAR where your program would need to clear the buffers at.

Example of the command:

Code: (Select All)
Print "Kindly play around with your keyboard some, and just press a few random keys for the next few seconds."
_Delay 5
Print "The keys you pressed were:"
Do
    k = _KeyHit
    If k > 0 Then i$ = InKey$: Print k, i$
Loop Until k = 0

Print
Print
Print "Well now, isn't that something?  Your keystrokes were recorded and reported several seconds AFTER you typed them into the keyboard, and affected your program even after your _DELAY!"
Print
Print "So how to prevent that type of issue?"
Print
Print "Captain _KEYCLEAR to the rescue!!!"
Print
Print "Press <ANY KEY> to see the solution in action."
Sleep
_KeyClear
Cls
Print "Kindly play around with your keyboard some, and just press a few random keys for the next few seconds."
_Delay 5
Print "The keys you pressed were:"
_KeyClear: Print "<<Captian _KEYCLEAR to the rescue here!!!>>"
Do
    k = _KeyHit
    If k > 0 Then i$ = InKey$: Print k, i$
Loop Until k = 0
 
As you can see from the simple example above, _DELAY doesn't stop our program from recording keypresses and adding them into our internal keyboard buffers.   (Neither does SLEEP, or several other "pause" type commands.)  Even though our program appears inactive, it's actually running the keyboard handlers in the background and collecting our last several keystrokes and saving those in a buffer for us.

How could that be bad for us?

Imagine you have a game which pauses execution with a series of set _DELAYs so it can shell out to a video player to play a cutscene for that game.  The user doesn't want to watch the cutscene, so they hit ESC, CTRL-F4, ALT-F4, and multiple other things to try and close the video early, only to find out the video doesn't support those keystrokes...  They watch the cutscene.  Control moves past the delays and back to your program.. Which now tries to process all those keystrokes in the buffer and...  BAM!!  Game closes without saving!!

AAARRRGGHHH!!!   (Who else just threw their controller through the monitor??  Fess up and admit it!  We've all been there. Tongue )

So, what's the easiest way to fix that type of issue?

Simply _KEYCLEAR after playing your cutscene and be done with it.  

Lots of programs have issues by reading unintended keystrokes from the buffers after a pause or delay.  The easiest way to fix those issues is with _KEYCLEAR.  It's your friend -- get to know him well.  Wink
Ahah!
Todays's pick is one I've just discovered (or rather, had pointed out to me) after struggling for a long time with used keypresses being resurrected.
Maybe it should be called the anti-phoenix, or anti-lazarus ommand?
It  clarified things further for me. Thanks Steve (and B-plus) for elucidating my hallucinations!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Reply




Users browsing this thread: 5 Guest(s)