仅允许条码扫描仪并消除键盘输入 [英] Allow Only Barcode Scanner and Eliminate Keyboard Input

查看:52
本文介绍了仅允许条码扫描仪并消除键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

由于我的条码模拟键盘工作,因此禁用我的常规键盘也会使我的条码扫描器无法工作.

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

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

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 毫秒内触发,所以它会读出.

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.

也尝试过下面的代码,但即使这样也不能按我的预期工作

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?

推荐答案

基本思路是检查:

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

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.

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

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.

请看下面:

public partial class BarcodeReader : Form
    {

        char cforKeyDown = '\0';
        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 == '\0')
            {
                cforKeyDown = '\0';
                _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;
        }
    }

点击这里.. GitHub 链接

这篇关于仅允许条码扫描仪并消除键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆