wpf 仅允许条码扫描仪并消除键盘输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50464858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:10:25  来源:igfitidea点击:

Allow Only Barcode Scanner and Eliminate Keyboard Input

c#.netwpfvb.netwinforms

提问by Rahul Verma

I have made a Windows Form application with a textbox which uses Barcode scanner to get any input value. I want user to use only Barcode Scanner to fill any value in it, and don't want to enter any input using my regular keyboard.

我制作了一个带有文本框的 Windows 窗体应用程序,它使用条形码扫描仪来获取任何输入值。我希望用户仅使用 Barcode Scanner 来填充其中的任何值,并且不想使用我的常规键盘输入任何输入。

Since my Barcode works mimics as a keyboard, so disabling my regular keyboard will also disable my Barcode scanner to work.

由于我的条码模拟键盘,所以禁用我的常规键盘也会使我的条码扫描仪无法工作。

I've searched manywhere to implement this, and found few answers were suggesting to add a Stopwatch/Timer to eliminiate all keypress which occurs within 50milliseconds, since Barcode can scan all values within 50 milliseconds, but no human can type faster than 50 miliseconds.

我搜索了很多地方来实现这一点,发现很少有答案建议添加秒表/计时器来消除在 50 毫秒内发生的所有按键,因为条码可以在 50 毫秒内扫描所有值,但没有人的输入速度可以超过 50 毫秒。

I also tried this way, but this fails when I randomly punches my fingers on keyboard keys, it reads out since some of keys fired within 50miliseconds.

我也尝试过这种方式,但是当我在键盘按键上随意敲击手指时失败了,因为某些按键在 50 毫秒内触发,所以它会读出。

Also tried below code but even this does not work as expected for me

还尝试了下面的代码,但即使这对我来说也没有按预期工作

private void rtBoxInput_KeyDown(object sender, KeyEventArgs e)
{
    e.SuppressKeyPress = true;
}

Please suggest some good way to implement this?

请提出一些实现这一点的好方法?

回答by Royal Blue Mayank

The basic idea is to check:

基本思想是检查:

if KeyUp and KeyDown events are fired of same keys and within specified time (say 17milliseconds), as this can be only done using Barcode scanner.

如果 KeyUp 和 KeyDown 事件在指定的时间(比如 17 毫秒)内被相同的键触发,因为这只能使用条形码扫描仪完成。

No one can trigger KeyDown and KeyUp event of same key within 17 milliseconds. For example it will take more than specified time for someone to Press and Release same key, however he can hit punch to keyboard that will push multiple keys all together and trigger their KeyDown and KeyUp events, but all no keys will have KeyUp and KeyDown events fired synchronously. So by this way you can detect whether input made by regular keyboard or barcode scanner.

没有人可以在 17 毫秒内触发同一键的 KeyDown 和 KeyUp 事件。例如,某人按下和释放相同的键将花费超过指定的时间,但是他可以敲击键盘,将多个键一起按下并触发它们的 KeyDown 和 KeyUp 事件,但所有没有键的键都会有 KeyUp 和 KeyDown 事件同步发射。因此,通过这种方式,您可以检测是否由常规键盘或条形码扫描仪输入。

Please have a look below:

请看下面:

public partial class BarcodeReader : Form
    {

        char cforKeyDown = '
     private void Form1_KeyPress(object sender, KeyPressEventArgs e)
     {
        barcode = string.Empty;

        try
        {
            barcode += e.KeyChar;

            if (lastTime > new DateTime())
            {
                if (DateTime.Now.Subtract(lastTime).Milliseconds > 30)
                {
                    f1 = false;
                }
                else
                {
                    f1 = true;
                }
            }

            lastTime = DateTime.Now;

            /*

            Test your Barcode, and if it matches your criteria then change your TextBox text

            TextBox1.Text = barcode;

            */

        }
        catch (Exception ex)
        {
            MessageBox.Show("Something went wrong");
        }
     }
'; int _lastKeystroke = DateTime.Now.Millisecond; List<char> _barcode = new List<char>(1); bool UseKeyboard = false; public BarcodeReader() { InitializeComponent(); } private void BarcodeReader_Load(object sender, EventArgs e) { this.KeyDown += new KeyEventHandler(BarcodeReader_KeyDown); this.KeyUp += new KeyEventHandler(BarcodeReader_KeyUp); } private void BarcodeReader_KeyUp(object sender, KeyEventArgs e) { // if keyboard input is allowed to read if (UseKeyboard && e.KeyData != Keys.Enter) { MessageBox.Show(e.KeyData.ToString()); } /* check if keydown and keyup is not different * and keydown event is not fired again before the keyup event fired for the same key * and keydown is not null * Barcode never fired keydown event more than 1 time before the same key fired keyup event * Barcode generally finishes all events (like keydown > keypress > keyup) of single key at a time, if two different keys are pressed then it is with keyboard */ if (cforKeyDown != (char)e.KeyCode || cforKeyDown == '##代码##') { cforKeyDown = '##代码##'; _barcode.Clear(); return; } // getting the time difference between 2 keys int elapsed = (DateTime.Now.Millisecond - _lastKeystroke); /* * Barcode scanner usually takes less than 17 milliseconds as per my Barcode reader to read , increase this if neccessary of your barcode scanner is slower * also assuming human can not type faster than 17 milliseconds */ if (elapsed > 17) _barcode.Clear(); // Do not push in array if Enter/Return is pressed, since it is not any Character that need to be read if (e.KeyCode != Keys.Return) { _barcode.Add((char)e.KeyData); } // Barcode scanner hits Enter/Return after reading barcode if (e.KeyCode == Keys.Return && _barcode.Count > 0) { string BarCodeData = new String(_barcode.ToArray()); if (!UseKeyboard) MessageBox.Show(String.Format("{0}", BarCodeData)); _barcode.Clear(); } // update the last key press strock time _lastKeystroke = DateTime.Now.Millisecond; } private void BarcodeReader_KeyDown(object sender, KeyEventArgs e) { //Debug.WriteLine("BarcodeReader_KeyDown : " + (char)e.KeyCode); cforKeyDown = (char)e.KeyCode; } }

Check Here.. GitHub Link

检查这里.. GitHub 链接

回答by vasily.sib

If your barcode mimics a keyboard - there is no way you can find which one is inputing text in your TextBox. Can your barcode scaner add some prefix to scanned code? If yes - I think this is a best option in combination with 50ms timer.

如果您的条形码模仿键盘 - 您将无法找到哪个正在您的 TextBox 中输入文本。您的条形码扫描仪可以为扫描的代码添加一些前缀吗?如果是 - 我认为这是结合 50ms 计时器的最佳选择。

回答by Noob dev

What you can do is to handle your Barcode directly on your form using a KeyPress Event and disable your TextBox :

您可以做的是使用 KeyPress 事件直接在表单上处理您的 Barcode 并禁用您的 TextBox :

##代码##

Don't forgot to set Form1.KeyPreview = true and it should do the trick !

不要忘记设置 Form1.KeyPreview = true ,它应该可以解决问题!