8

How do I flush the keyboard buffer in C# using Windows Forms?

I have a barcode scanner which acts like a keyboard. If a really long barcode is scanned and the cancel button is hit on the form, I need the keyboard buffer to be cleared. So I need to flush and ignore all pending input. I need the buffer cleared because if the barcode contains spaces, the spaces are processed as button clicks which is unecessary.

Amar Premsaran Patel
  • 1,293
  • 7
  • 17
  • 26
  • What do you mean by keyboard buffer? Do you want to flush and ignore all pending input that you haven't processed yet? – Michael Jun 10 '09 at 20:55
  • Why are you trying to do that? – SLaks Jun 10 '09 at 21:05
  • Yes...for example I have a barcode scanner which acts like a keyboard...if a really long barcode is scanned and the cancel button is hit on the form, I need the keyboard buffer to be cleared. So I need to flush and ignore all pending input. – Amar Premsaran Patel Jun 10 '09 at 21:06
  • I need the buffer cleared because if the barcode contains spaces, the spaces are processed as button clicks which is unecessary. – Amar Premsaran Patel Jun 10 '09 at 21:08

7 Answers7

5
while (Console.KeyAvailable) { Console.ReadKey(true); }
a stone arachnid
  • 1,272
  • 1
  • 15
  • 27
Chris
  • 51
  • 1
  • 1
2

Set KeyPreview on the Form to true, then catch the KeyPress event and set e.Handled to true if cancel was clicked.

EDIT: Catch the Form's KeyPress event

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

@Chris:

Console.KeyAvailable threw an exception on me because the Console did not have focus.

The exception message was helpful, though. It suggested using Console.In.Peek() to read in that instance.

I thought it would be worth noting here as an alternate solution and for posterity.

 if (-1 < Console.In.Peek()) {
     Console.In.ReadToEnd();
 }
1

Disable the form, and force all processing to occur with DoEvents whilst it's disabled. The Controls should reject any keystokes because they are disabled. Then re-enable the form.

this.Enabled = false;
Application.DoEvents();
this.Enabled = true;
midspace
  • 922
  • 9
  • 18
1

I'm not sure you can do this. The keystrokes go into the event queue for the main event loop. Any action you take to cancel these keystrokes will be placed in the queue after the keystrokes.

The event loop will only get to your cancel action after the keystrokes have been processed. You can only cancel the keystrokes based on some event that happens in the middle of the keystroke sequence.

rein
  • 32,967
  • 23
  • 82
  • 106
0

It's a old question but I got the same issue and I found a good way to do it...
When Application.DoEvent() is called, the keystoke buffer call the KeyPress event but _keypress is set to false, so it'll skip all them, after that _keypress return to true to be ready for another keypress event !

public bool _keypress;
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!_keypress)
    {
        _keypress = true;
        // instructions
    }
    Application.DoEvents();
    _keypress = false;
}
BlinkSun
  • 75
  • 2
  • 8
0

You could do BIOS level flushing http://support.microsoft.com/kb/43993 but I would advise against this low level approach since Windows also does keyboard buffering on a higher level.

The best way seems to be to eat any arriving char while a specific flag is set. As SLaks suggests I would set KeyPreview to true and set e.Handled to true either in KeyPress or in KeyDown.. should make no difference.

Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
VVS
  • 19,405
  • 5
  • 46
  • 65