简体   繁体   English

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

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

I have a USB barcode scanner attached. 我连接了USB条码扫描器。 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. 我目前正在通过在发送条形码之前先发送特殊的组合键,来检测是否已通过键盘将按键发送到文本框。

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" eg 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. 我只是认为,这样做可能比从条形码扫描仪发送特殊的组合键并使用它更整洁。

e.KeyboardDevice的属性

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

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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM