C# 或 .NET 刷新键盘缓冲区

发布于 2024-07-23 05:22:01 字数 173 浏览 9 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

笑饮青盏花 2024-07-30 05:22:01
while (Console.KeyAvailable) { Console.ReadKey(true); }
while (Console.KeyAvailable) { Console.ReadKey(true); }
迷爱 2024-07-30 05:22:01

将表单上的 KeyPreview 设置为 true,然后捕获 KeyPress 事件并将 e.Handled 设置为 如果单击“取消”,则为 true

编辑:捕获表单的 KeyPress 事件

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

九命猫 2024-07-30 05:22:01

我不确定你能做到这一点。 击键进入主事件循环的事件队列。 您为取消这些击键而采取的任何操作都将在击键之后放入队列中。

事件循环仅在处理完击键后才会执行取消操作。 您只能根据击键序列中间发生的某些事件来取消击键。

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.

裸钻 2024-07-30 05:22:01

@Chris:

Console.KeyAvailable 向我抛出异常,因为控制台没有焦点。

不过,异常消息很有帮助。 它建议使用Console.In.Peek() 来读取该实例。

我认为这里值得注意的是作为替代解决方案和子孙后代。

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

@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();
 }
傲世九天 2024-07-30 05:22:01

禁用表单,并在禁用时强制使用 DoEvents 进行所有处理。 控件应拒绝任何按键,因为它们已被禁用。
然后重新启用该表单。

this.Enabled = false;
Application.DoEvents();
this.Enabled = true;

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;
毁我热情 2024-07-30 05:22:01

可以进行BIOS级别刷新http://support.microsoft.com/kb/43993 但我建议不要使用这种低级方法,因为 Windows 也在更高级别上进行键盘缓冲。

最好的方法似乎是在设置特定标志时吃掉任何到达的字符。 正如 SLaks 建议的那样,我将在 KeyPressKeyDown 中将 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 set e.Handled to true either in KeyPress or in KeyDown.. should make no difference.

浅唱々樱花落 2024-07-30 05:22:01

这是一个老问题,但我遇到了同样的问题,并且我找到了一个好方法......
当调用 Application.DoEvent() 时,按键缓冲区会调用 KeyPress 事件,但 _keypress 设置为 false,因此它将跳过所有这些事件,之后 _keypress 返回 true 准备再次按键事件 !

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文