Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DAY 006: _KEYCLEAR
#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
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply


Messages In This Thread
DAY 006: _KEYCLEAR - by SMcNeill - 11-11-2022, 04:33 PM
RE: DAY 006: _KEYCLEAR - by Pete - 11-11-2022, 06:55 PM
RE: DAY 006: _KEYCLEAR - by PhilOfPerth - 11-11-2022, 11:49 PM



Users browsing this thread: 1 Guest(s)