C# 或 .NET 刷新键盘缓冲区
如何使用 Windows 窗体在 C# 中刷新键盘缓冲区?
我有一个条形码扫描仪,其作用类似于键盘。 如果扫描了很长的条形码并按下了表单上的取消按钮,我需要清除键盘缓冲区。 所以我需要刷新并忽略所有待处理的输入。 我需要清除缓冲区,因为如果条形码包含空格,则空格将作为按钮单击进行处理,这是不必要的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将表单上的
KeyPreview
设置为true
,然后捕获KeyPress
事件并将e.Handled
设置为如果单击“取消”,则为 true
。编辑:捕获表单的 KeyPress 事件
Set
KeyPreview
on the Form totrue
, then catch theKeyPress
event and sete.Handled
totrue
if cancel was clicked.EDIT: Catch the Form's KeyPress event
我不确定你能做到这一点。 击键进入主事件循环的事件队列。 您为取消这些击键而采取的任何操作都将在击键之后放入队列中。
事件循环仅在处理完击键后才会执行取消操作。 您只能根据击键序列中间发生的某些事件来取消击键。
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.
@Chris:
Console.KeyAvailable 向我抛出异常,因为控制台没有焦点。
不过,异常消息很有帮助。 它建议使用Console.In.Peek() 来读取该实例。
我认为这里值得注意的是作为替代解决方案和子孙后代。
@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.
禁用表单,并在禁用时强制使用 DoEvents 进行所有处理。 控件应拒绝任何按键,因为它们已被禁用。
然后重新启用该表单。
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.
您可以进行BIOS级别刷新http://support.microsoft.com/kb/43993 但我建议不要使用这种低级方法,因为 Windows 也在更高级别上进行键盘缓冲。
最好的方法似乎是在设置特定标志时吃掉任何到达的字符。 正如 SLaks 建议的那样,我将在
KeyPress
或KeyDown
中将KeyPreview
设置为 true 并将e.Handled
设置为 true ..应该没有什么区别。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 sete.Handled
to true either inKeyPress
or inKeyDown
.. should make no difference.这是一个老问题,但我遇到了同样的问题,并且我找到了一个好方法......
当调用 Application.DoEvent() 时,按键缓冲区会调用 KeyPress 事件,但 _keypress 设置为 false,因此它将跳过所有这些事件,之后 _keypress 返回 true 准备再次按键事件 !
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 !