Ciao!
Secondo me hai detto bene... devi separare le due cose! ovvero una cosa è l'input da tastiera, e una cosa è il lettore in emulazione... come? semplice : tu sai che la digitazione da tastiera normalmente viene fatta premendo un tasto dopo l'altro ad una velocità media di 2-3 caratteri al secondo, nei casi più estremi si arriva a 6-7, forse 8, quindi? il lettore barcode in emulazione tastiera scrive tutti i caratteri entro poche frazioni di secondi (anche se comunque lo fa ovviamente una dopo l'altra). quindi sostanzialmente ti basta controllare il numero di caratteri digitati entro un tot di tempo per poter capire se la digitazione avviene tramite "essere umano" oppure tramite emulatore. Ovviamente per fare ciò ho paura che con gli eventi form non riesca perchè probabilmente poco performanti, quindi eccoti un esempio di come potresti fare :
codice:
public class BarCodeListener : IDisposable
{
bool Run { get; set; }
public int TimeOutBarCode { get; set; }
public string BarcodeEscape { get; set; }
public int BarCodeLength { get; set; }
bool enable = false;
public bool Enable
{
get { return this.enable; }
set
{
if (value)
{
Stop();
Start();
}
else
{
Stop();
}
this.enable = value;
}
}
KeyPressEventHandler keyPressEventHandler = null;
BarCodeEventHandler barCodeEventHandler = null;
StringBuilder sb = new StringBuilder();
bool TimerInUse = false;
public BarCodeListener()
{
}
void Start()
{
Thread t = new Thread(RunBarCode);
Run = true;
t.Start();
}
void Stop()
{
this.Run = false;
TimerInUse = false;
}
void timerCode()
{
//while (TimerInUse)
{
int time = 0;
int timeADD = 10;
while (time < this.TimeOutBarCode)
{
Thread.Sleep(timeADD);
time += timeADD;
}
string txt = sb.ToString();
if (txt.Length == BarCodeLength + 1 && txt.Contains(BarcodeEscape))
{
this.OnBarCodeEnter(txt);
}
sb.Remove(0, sb.Length);
TimerInUse = false;
}
}
void RunBarCode()
{
while (Run)
{
for (int i = 1; i < Byte.MaxValue; i++)
{
if (GetAsyncKeyState(i) == Int16.MinValue + 1)
{
OnKeyPress(new KeyPressEventArgs(Control.ModifierKeys, i));
if (!TimerInUse)
{
TimerInUse = true;
Thread t2 = new Thread(timerCode);
t2.Start();
}
sb.Append(Convert.ToChar(i));
}
}
Thread.Sleep(5);
}
}
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(int vKey);
protected virtual void OnKeyPress(KeyPressEventArgs KeyPressInfo)
{
if (keyPressEventHandler != null)
{
keyPressEventHandler(this, KeyPressInfo);
}
}
protected virtual void OnBarCodeEnter(string BarCode)
{
if (barCodeEventHandler != null)
{
barCodeEventHandler(this, BarCode);
}
}
public delegate void KeyPressEventHandler
(object inputListener,
KeyPressEventArgs KeyPressInfo);
public delegate void BarCodeEventHandler
(object inputListener,
string BarCode);
public event KeyPressEventHandler KeyPress
{
add { this.keyPressEventHandler += value; }
remove { this.keyPressEventHandler -= value; }
}
public event BarCodeEventHandler BarCodeEnter
{
add { this.barCodeEventHandler += value; }
remove { this.barCodeEventHandler -= value; }
}
#region IDisposable Membri di
public void Dispose()
{
this.Enable = false;
}
#endregion
}
public class KeyPressEventArgs : EventArgs
{
public KeyPressEventArgs(Keys ModifierKeys,
int KeyCode)
{
this.ModifierKeys = ModifierKeys;
this.KeyCode = KeyCode;
}
public readonly Keys ModifierKeys;
public readonly int KeyCode;
}
A questo punto con questo codice, che inoltre rimane sempre attivo anche quando non sei sulla textbox o il form non ha il focus (almeno con la pistola che ho usato per provare va tranquillamente ma non dovrebbero esserci differenze), puoi rimanere in attesa di evento e farne quello che vuoi con il barcode rilevato, ad esempio scriverlo sulla textBox...
ecco un esempio di come potresti utilizzarlo :
codice:
BarCodeListener barCode
..
//imposti la lunghezza del barcode (qui 12) il carattere di escape ("\r")
//e il tempo massimo (qui 1000 = 1 sec.) che ci deve mettere per scrivere tutti i caratteri
barCode = new BarCodeListener()
{
BarCodeLength = 12,
BarcodeEscape = "\r",
TimeOutBarCode = 1000
};
barCode.BarCodeEnter += new BarCodeListener.BarCodeEventHandler(barCode_BarCodeEnter);
barCode.Enable = true;
...
void barCode_BarCodeEnter(object inputListener, string BarCode)
{
textBox.Text = BarCode;
}
...
public void Dispose()
{
barCode.Dispose();
}
spero ti sia utile...

EDIT : in ogni caso, qualora l'operatore sia flash scrivendo sulla tastiera, puoi sempre attivare e disattivare l'oggetto BarCodeListener con .Enable = true/false;