C# or .NET Flushing Keyboard Buffer

后端 未结 7 1358
太阳男子
太阳男子 2021-01-12 08:34

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

7条回答
  •  执笔经年
    2021-01-12 09:09

    @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();
     }
    

提交回复
热议问题