11-11-2022, 11:49 PM
(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.Ahah!
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 "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 "So how to prevent that type of issue?"
Print "Captain _KEYCLEAR to the rescue!!!"
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. )
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.
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.)