The barcode scanner is a bit similar external keyboard (actually it is equivalent to a keyboard from messaging), the input focus to the type of control, a scan of the corresponding bar code information is input into the text box, but without input focus, or another irrelevant programs get the input focus, it's a little bit confused, I want to achieve is that no matter what, as long as the scanner is a job, my program can automatically activated, and can get the current input bar code information, implementation approach: I use the USB port of barcode scanners, careful analysis once, after the success of the scan, in the form of a keyboard key message notification bar code input information to the system, so by the keyboard hook can convenient access to the information, however, how to distinguish between information is a keyboard or bar code input? Is very simple, barcode scanner in a very short period of time input for at least 3 characters or more information, and ends with the "enter" character, it is in this spirit, perfect implementation of the function of reservation,
Form the relevant code:
The view plaincopy to clipboardprint?
10 · · · · · · · · · · · · · · · · · · · · · · · · · 20 and 30 40 · · · · · · · · · · · · · · · · · · · · · · · · s · · · · · · · · 70 · · · · · · · · 80 · · · · · · · · 90 · · · · · · · · 100 · · · · · · · 110 · · · · · · · 120 · · · · · · · 130 · · · · · · · 140 · · · · · · · 150
Using System;
UsingSystem. Collections. Generic;
UsingSystem.Com ponentModel;
Using System. The Data;
Using System. Drawing;
Using System. The Text;
UsingSystem. Windows. Forms;
The namespace ReadBadCode
{
Publicpartial class frmTest: Form
{
BarCodeHook BarCode=newBarCodeHook ();
Public frmTest ()
{
InitializeComponent ();
BarCode. BarCodeEvent +=newBarCodeHook. BarCodeDelegate (BarCode_BarCodeEvent);
}
Private delegate void ShowInfoDelegate (BarCodeHook BarCodesbarCode);
Private void ShowInfo (BarCodeHook BarCodesbarCode)
{
If (this. InvokeRequired)
{
Enclosing the BeginInvoke (new ShowInfoDelegate (ShowInfo), the new object [] {barCode});
}
The else
{
TextBox1. Text=barCode. KeyName;
TextBox2. Text=barCode. VirtKey. ToString ();
TextBox3. Text=barCode. ScanCode. ToString ();
TextBox4. Text=barCode. AscII. ToString ();
TextBox5. Text=barCode. CRH. The ToString ();
TextBox6. Text=barCode. IsValid? BarCode. BarCode: "";
}
}
Using System;
using System.Collections.Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
Using System. The Text;
Using System. Windows. Forms;
The namespace ReadBadCode
{
Publicpartial class frmTest: Form
{
BarCodeHook BarCode=new BarCodeHook ();
Public frmTest ()
{
InitializeComponent ();
BarCode. BarCodeEvent +=newBarCodeHook. BarCodeDelegate (BarCode_BarCodeEvent);
}
Private delegate void ShowInfoDelegate (BarCodeHook BarCodesbarCode);
Private void ShowInfo (BarCodeHook BarCodes barCode)
{
If (this. InvokeRequired)
{
Enclosing the BeginInvoke (new ShowInfoDelegate (ShowInfo), the new object [] {barCode});
}
The else
{
TextBox1. Text=barCode. KeyName;
TextBox2. Text=barCode. VirtKey. ToString ();
TextBox3. Text=barCode. ScanCode. ToString ();
TextBox4. Text=barCode. AscII. ToString ();
TextBox5. Text=barCode. CRH. The ToString ();
TextBox6. Text=barCode. IsValid? BarCode. BarCode: "";
}
}
Void BarCode_BarCodeEvent (BarCodeHook BarCodes barCode)
{
ShowInfo (barCode);
}
Private void frmTest_Load (object sender, EventArgs e)
{
BarCode. Start ();
}
Private void frmTest_FormClosed (object sender, FormClosedEventArgse)
{
BarCode. Stop ();
}
Private void textBox6_TextChanged (object sender, EventArgs e)
{
If (textBox6. Text. Length & gt; 0)
{
MessageBox. Show (textBox6. Text);
}
}
}
}
BarCodeHook categories:
The view plaincopy to clipboardprint?
10 · · · · · · · · · · · · · · · · · · · · · · · · · 20 and 30 40 · · · · · · · · · · · · · · · · · · · · · · · · s · · · · · · · · 70 · · · · · · · · 80 · · · · · · · · 90 · · · · · · · · 100 · · · · · · · 110 · · · · · · · 120 · · · · · · · 130 · · · · · · · 140 · · · · · · · 150
Using System;
UsingSystem. Collections. Generic;
Using System. The Text;
UsingSystem. Runtime. InteropServices;
Using System. The Reflection;
The namespace ReadBadCode
{
Public classBarCodeHook
{
Public delegate void BarCodeDelegate (BarCodesbarCode);
The public event BarCodeDelegateBarCodeEvent;
Public struct BarCodes
{
Public intVirtKey;//virtual code
Public intScanCode;//scan code
Public string KeyName;//key name
Public uintAscII;//AscII
Public charChr;//character
Public string BarCode;//barcode information
Public boolIsValid;//bar code is valid
Public DateTimeTime;//scan time
}
Private struct EventMsg
{
Public int the message;
Public int paramL;
Public int paramH;
Public int Time;
Public int HWND;
}
[DllImport (" user32. DLL, "CharSet=CharSet. Auto, CallingConvention=CallingConvention. StdCall)]
Private static extern int SetWindowsHookEx (int idHook, HookProclpfn, IntPtr hInstance, intthreadId);
[DllImport (" user32. DLL, "CharSet=CharSet. Auto, CallingConvention=CallingConvention. StdCall)]
Private static extern bool UnhookWindowsHookEx (intidHook);
[DllImport (" user32. DLL, "CharSet=CharSet. Auto, CallingConvention=CallingConvention. StdCall)]
Private static extern int CallNextHookEx (int idHook, int nCode, Int32 wParam, IntPtrlParam);
[DllImport (" user32, "EntryPoint=" GetKeyNameText ")]
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull