WPF c# 检测通过按键发送的键盘(以检测它是否是条形码扫描仪)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21002657/
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 10:27:18  来源:igfitidea点击:

WPF c# Detect which keyboard sent through a key press (to detect if it was barcode scanner)

c#wpf

提问by DermFrench

I have a USB barcode scanner attached. I'm currently detecting whether key presses were sent to a text box from it via keyups, by having it send a special key combination before it sends the barcode.

我有一个 USB 条码扫描器。我目前正在检测按键是否通过 keyups 发送到文本框,方法是让它在发送条形码之前发送一个特殊的组合键。

However I was wondering if there is another way to do it.

但是我想知道是否有另一种方法可以做到这一点。

I took at the KeyEventArgs

我参加了 KeyEventArgs

private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
    this.TextBlock1.Text = e.KeyboardDevice.ToString();
}

I thought e.KeyboardDevice might give me some info on which "keyboard" e.g. the standard keyboard or the "usb barcode scanner keyboard" but I can't seem to find any of that information.

我认为 e.KeyboardDevice 可能会给我一些关于“键盘”的信息,例如标准键盘或“USB 条码扫描仪键盘”,但我似乎找不到任何这些信息。

I just thought there may be a neater way of doing this than sending the special key combination from the barcode scanner and using that.

我只是认为可能有一种比从条形码扫描仪发送特殊组合键并使用它更简洁的方法。

Properties of e.KeyboardDevice

e.KeyboardDevice 的属性

回答by Shravan Sunder

I've used this answer to solve the same problem in the past. The answer contains a link with full details about the solution. https://stackoverflow.com/a/589326/2696641

我过去曾使用此答案来解决相同的问题。答案包含一个链接,其中包含有关解决方案的完整详细信息。 https://stackoverflow.com/a/589326/2696641

回答by Paul Trujillo

I thought Id contribute my solution. Its not that elegant but it only looks for numeric key presses and then looks at the time it takes to respond. If the time is longer than the maximum threshold then it throws out those values from the array. I hope this helps someone.

我认为我贡献了我的解决方案。它不是那么优雅,但它只查找数字按键,然后查看响应所需的时间。如果时间长于最大阈值,则它会从数组中丢弃这些值。我希望这可以帮助别人。

class BarcodeReader
{
    ArrayList barCode = new ArrayList();
    ArrayList barCodeTimes = new ArrayList();
    ArrayList barCodeDeltaTimes = new ArrayList();
    /// <summary>
    /// Input 1: delayTime (ms) - time for scanner to return values (threshold)[30 seems good],
    /// Input 2: KeyEventArgs - put in key [this.KeyDown += new KeyEventHandler(Form1_KeyDown)],
    /// Output 1: String of barcode read
    /// </summary>
    /// <param name="delayTime"></param>
    /// <param name="e"></param>
    /// <returns></returns>
    public string BarcodeValue(int delayTime, KeyEventArgs e)
    {
        string barCodeString = null;
        var isNumber = e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9;
        var rtn = e.KeyCode == Keys.Enter;

        if (isNumber)
        {
            barCode.Add(Encoding.ASCII.GetString(new byte[] { (byte)e.KeyValue }));
            barCodeTimes.Add(DateTime.Now.TimeOfDay.TotalMilliseconds);
        }
        if (rtn)
        {
            barCodeString = ValuesToString(delayTime);
        }
        return barCodeString;
    }
    private string ValuesToString(int delayTime)
    {

        string barCodeString = null;

        foreach (double d in barCodeTimes)
        {
            double diff = 0;
            int index = barCodeTimes.IndexOf(d);
            if (index < barCodeTimes.Count - 1)
            {
                diff = (double)barCodeTimes[index + 1] - (double)barCodeTimes[index];
            }

            barCodeDeltaTimes.Add(diff);
        }
        foreach (double d in barCodeDeltaTimes)
        {
            if (d > delayTime)
            {
                barCode.RemoveAt(0);
            }
        }
        foreach (string s in barCode)
        {
            barCodeString += s;
        }

        barCode.Clear();
        barCodeTimes.Clear();
        barCodeDeltaTimes.Clear();

        return barCodeString;
    }

}