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