0

I have a plug and play Barcode scanner. The Barcode scanner return data at the cursor position(as HID Device). The data can be captured from any application with the focus. I don't have a text box in my application so i couldn't set the focus on my application and the data must be captured only in my application(Windows form). I'm not sure where should i start. Any help would be helpful.

Code As if now

DateTime lastKeystroke = new DateTime(0);
List<char> barcode = new List<char>(10);
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        // check timing (keystrokes within 100 ms)
        TimeSpan elapsed = (DateTime.Now - lastKeystroke);
        if (elapsed.TotalMilliseconds > 100)
            barcode.Clear();

        // record keystroke & timestamp
        barcode.Add(e.KeyChar);
        lastKeystroke = DateTime.Now;

        // process barcode
        if (e.KeyChar == 13 && _barcode.Count > 0)
        {
            string msg = new String(barcode.ToArray());
            MessageBox.Show(msg); //I get Value here
            barcode.Clear();
        }
    }

This works fine. but the problem is when my form is in Focus i get data i can work with it. but when my form doesn't have focus, meaning if it is minimized or the Some other application is opened , i don't get the data

SH7
  • 732
  • 7
  • 20
  • does the scanner has an API too? – pm100 Nov 21 '16 at 17:30
  • @pm100 : No the Barcode scanner doesn't have any API. – SH7 Nov 21 '16 at 17:32
  • Barcode scanners usually act like a keyboard and send keystrokes, so you can check when the focus is on a `TextBox` and you scan something, the text should be written in `TextBox`. – Reza Aghaei Nov 21 '16 at 17:50
  • @RezaAghaei : Thank you for your reply. as u said i use keypress event and i get data when my form is in Focus. but i don't get when my application doesn't have focus. I have updated the question. – SH7 Nov 21 '16 at 18:02
  • You're not going to get any keyboard input (including keypress) if your app does not have the focus and a place to put keyboard input. The foreground application (the one that *does* have the focus) receives all input. – Ken White Nov 21 '16 at 18:04
  • Since most of scanners just behave like a keyboard you could use any technique that allows you to capture keystrokes without having focus. Global hooks comes to mind but that is pretty nasty. – Frank J Nov 21 '16 at 18:04
  • @Frank J : Regarding Global hooks can you give me some reference and idea to use in my case. – SH7 Nov 21 '16 at 18:12
  • [Here you go](http://stackoverflow.com/questions/604410/global-keyboard-capture-in-c-sharp-application) – Frank J Nov 21 '16 at 18:13
  • @Ken White : Is there a way where i could get the device Id or some unique field by which i could know if the keypress is from keyboard or some other device. – SH7 Nov 21 '16 at 18:14
  • @user3501749: Most scanners don't unless you can configure your scanner to send one. As far as Windows is concerned, keyboard input is keyboard input. – Ken White Nov 21 '16 at 18:16

0 Answers0