1

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.

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

I took at the 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.

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

DermFrench
  • 3,968
  • 13
  • 43
  • 70
  • 2
    possible duplicate of [How to distinguish between multiple input devices in C#](http://stackoverflow.com/questions/587840/how-to-distinguish-between-multiple-input-devices-in-c-sharp) – DermFrench Jan 09 '14 at 10:46

2 Answers2

0

You could determine it by timing, if time between keypresses is less than 50ms, then it's Scanner

private readonly Stopwatch _stopwatch;
private long _lastKeyPressedAgo;

public Constructor()
{
   _stopwatch = new Stopwatch();
   _stopwatch.Start();
   _lastKeyPressedAgo = -1;
}

private async Task KeyDown(RoutedEventArgs routedEvent)
{
   if (routedEvent is not KeyEventArgs keyEventArgs)
   {
      return;
   }

    _stopwatch.Stop();
    _lastKeyPressedAgo = _stopwatch.ElapsedMilliseconds;
    _stopwatch.Restart();

    if (_lastKeyPressedAgo is > 0 and < 50)
    {
       //This means it's from scanner 
    }
}

Something like this should work. Although in this case, first key press will not be registered, but you could think of workarounds for it, save it in variable for example, and then if you confirm it's scanner, you know what first press was.

Emilis Vadopalas
  • 1,019
  • 2
  • 14
  • 22
-4

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

}
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58