We help IT Professionals succeed at work.

Flushing keyboard buffer

Barnabas3
Barnabas3 asked
on
Medium Priority
3,211 Views
Last Modified: 2008-02-01
How can i flush the keyboard buffer from a vb program?
Comment
Watch Question

Nitin SontakkeDeveloper
CERTIFIED EXPERT

Commented:
Please see help in MSDN under topic "SendKeys Statement".

Example straight from MSDN is as follows:

Dim ReturnValue, I
ReturnValue = Shell("CALC.EXE", 1)   ' Run Calculator.
AppActivate ReturnValue    ' Activate the Calculator.
For I = 1 To 100   ' Set up counting loop.
   SendKeys I & "{+}", True   ' Send keystrokes to Calculator
Next I   ' to add each value of I.
SendKeys "=", True   ' Get grand total.
SendKeys "%{F4}", True   ' Send ALT+F4 to close Calculator.

I didn't find any way to flush it. However, there does seem to be a .Flush method to SendKeys class in .NET
What keyboard buffer are you refering to.  In a VB program, there is no "keyboard" buffer, per se - a concept which is a carry-over from the days of Mainframe "dumb" terminals which did not actually process the "Keyboard buffer" until the Enter Key was depressed.  In a Windows Program, ALL keystrokes are processed as they are pressed, by the Operating system - THey KeyPress is treated by Windows as a "message" to be handled by the relevant "window".  In your VB program, you can handle each keypress using the KeyPress Event of the relevant control (or form)

Arthur Wood

Commented:
If you have some heavy processing in a loop (math, graphics), your app won't receive keyboard (or mouse) events.
To allow receiving these events, you can add DoEvents to your loop:
Do
   ' do some processing
   ' ...

   DoEvents
Loop

That will work for most cases. But calling DoEvents in each pass can be a bit expensive.
It will also check and allow processing of other low-priority messages (like paint messages)

What you can do, is - call DoEvents only if there is some input message waiting:
Do
   ' some processing

   If GetInputState() <> 0 Then
      ' we have some input message (keyboard or mouse) waiting
      DoEvents
   End If
Loop

or if you want to call DoEvents only when there is keyboard message:
Do
   ' some processing

   If GetQueueStatus(QS_KEY) <> 0 Then
      ' keyboard message waiting in a queue
      DoEvents
   End If
Loop

Some API declarations:
Private Declare Function GetInputState Lib "user32" () As Long
Private Declare Function GetQueueStatus Lib "user32" (ByVal fuFlags As Long) As Long
Private Const QS_KEY = &H1
CERTIFIED EXPERT
Author of the Year 2009

Commented:
Hi Barnabas3,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Save as PAQ -- No Refund.

Barnabas3, Please DO NOT accept this comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
==========
DanRollins -- EE database cleanup volunteer
Per recommendation, points NOT refunded and question closed.

Netminder
CS Moderator