【目前的條形碼掃描器有點(diǎn)類似外接鍵盤(其實(shí)從消息傳送上它就相當(dāng)于一個(gè)鍵盤),,把輸入焦點(diǎn)定位到可輸入的控件上,一掃描相應(yīng)的條形碼信息就輸入到文本框中去了,,但是如果沒有輸入焦點(diǎn),,或另一個(gè)不相干的程序獲得輸入焦點(diǎn),,那就有點(diǎn)亂套了。我想實(shí)現(xiàn)的是,,不管什么情況,,只要掃描器一工作,,我的程序就能自動(dòng)激活,并能獲得當(dāng)前輸入的條形碼信息,。 實(shí)現(xiàn)思路:我用的是litele牌的USB口的紅外條形碼掃描器,,仔細(xì)分析了一下,掃描成功后,,以鍵盤按鍵消息的形式把條形碼輸入信息通知給系統(tǒng),。這樣通過鍵盤鉤子就可以方便的獲得該信息了。但是,,怎樣區(qū)分信息是鍵盤還是條形碼輸入的哪,?很簡(jiǎn)單,條形碼掃描器在很短的時(shí)間內(nèi)輸入了至少3個(gè)字符以上信息,,并且以“回車”作為結(jié)束字符,,在這種思想指引下,很完美的實(shí)現(xiàn)了預(yù)定功能,?!?/p>
frmMain:
- public BarCodeHook BarCode = new BarCodeHook();
- public delegate void ShowInfoDelegate(BarCodeHook.BarCodes barCode);
- void ShowInfo(BarCodeHook.BarCodes barCode){
- textBox_barCode.Text = barCode.BarCode;
- buttonX2.Focus();
- }
- public void BarCode_BarCodeEvent(BarCodeHook.BarCodes barCode)
- {
- ShowInfo(barCode);
- }
-
-
- public frmMain()
- {
- InitializeComponent();
- BarCode.BarCodeEvent += new BarCodeHook.BarCodeDelegate(BarCode_BarCodeEvent);
-
- }
如果焦點(diǎn)本來就在textBox上,會(huì)產(chǎn)生多余的字符,,所以在showInfo函數(shù)里,,每次都手動(dòng)讓buttonX2成為焦點(diǎn)。
- public class BarCodeHook
- {
- public delegate void BarCodeDelegate(BarCodes barCode);
- public event BarCodeDelegate BarCodeEvent;
-
- public struct BarCodes
- {
- public int VirtKey; //虛擬碼
- public int ScanCode; //掃描碼
- public string KeyName; //鍵名
- public uint AscII; //AscII
- public char Chr; //字符
-
- public string BarCode; //條碼信息
- public bool IsValid; //條碼是否有效
- public DateTime Time; //掃描時(shí)間
- }
-
- private struct EventMsg
- {
- public int 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, HookProc lpfn, IntPtr hInstance, int threadId);
-
- [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
- private static extern bool UnhookWindowsHookEx(int idHook);
-
- [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
- private static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
-
- [DllImport("user32", EntryPoint = "GetKeyNameText")]
- private static extern int GetKeyNameText(int lParam, StringBuilder lpBuffer, int nSize);
-
- [DllImport("user32", EntryPoint = "GetKeyboardState")]
- private static extern int GetKeyboardState(byte[] pbKeyState);
-
- [DllImport("user32", EntryPoint = "ToAscii")]
- private static extern bool ToAscii(int VirtualKey, int ScanCode, byte[] lpKeyState, ref uint lpChar, int uFlags);
-
- delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
- BarCodes barCode = new BarCodes();
- int hKeyboardHook = 0;
- public string strBarCode = "";
- public int length;
-
- private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
- {
- barCode.IsValid = false;
- bool notChar = false;
- if (nCode == 0)
- {
- EventMsg msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof(EventMsg));
-
- if (wParam == 0x100) //WM_KEYDOWN = 0x100
- {
- barCode.VirtKey = msg.message & 0xff; //虛擬碼
- barCode.ScanCode = msg.paramL & 0xff; //掃描碼
-
- StringBuilder strKeyName = new StringBuilder(255);
- if (GetKeyNameText(barCode.ScanCode * 65536, strKeyName, 255) > 0)
- {
- barCode.KeyName = strKeyName.ToString().Trim(new char[] { ' ', '\0' });
- }
- else
- {
- barCode.KeyName = "";
- }
-
- byte[] kbArray = new byte[256];
- uint uKey = 0;
- GetKeyboardState(kbArray);
- if (ToAscii(barCode.VirtKey, barCode.ScanCode, kbArray, ref uKey, 0))
- {
- barCode.AscII = uKey;
- barCode.Chr = Convert.ToChar(uKey);
- }
- else
- {
- notChar = true; //轉(zhuǎn)到ascii字符失敗,,這不是一個(gè)正常字符,,要去掉
- }
-
-
- if (DateTime.Now.Subtract(barCode.Time).TotalMilliseconds > 30) //30ms可以過濾掉連續(xù)按住一個(gè)鍵時(shí)的情況
- {
- if (notChar == false)
- strBarCode = barCode.Chr.ToString();
- else
- strBarCode = "";
- barCode.IsValid = false;
- }
- else
- {
- if (strBarCode.Length >= 5)
- {
- barCode.IsValid = true; //isValid為true表明這是個(gè)條碼
- }
- if (notChar == false)
- {
- strBarCode += barCode.Chr.ToString();
- }
- barCode.BarCode = strBarCode;
- }
-
-
-
-
-
- barCode.Time = DateTime.Now;
- if (BarCodeEvent != null && barCode.IsValid) BarCodeEvent(barCode); //觸發(fā)事件
-
- }
- }
- return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
- }
-
- // 安裝鉤子
- public bool Start()
- {
- if (hKeyboardHook == 0)
- {
- //WH_KEYBOARD_LL = 13
- hKeyboardHook = SetWindowsHookEx(13, new HookProc(KeyboardHookProc), Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
- }
- return (hKeyboardHook != 0);
- }
-
- // 卸載鉤子
- public bool Stop()
- {
- if (hKeyboardHook != 0)
- {
- bool result = UnhookWindowsHookEx(hKeyboardHook);
- hKeyboardHook = 0; //將hKeyboardHook 置為0
- if (result)
- {
-
- //MessageBox.Show("true");
- }
- return result;
- }
-
- return true;
- }
- }
是如果掃到的是英文字符的話,會(huì)有一個(gè)多余的碼無法從鍵盤碼轉(zhuǎn)到ascii碼,,需要去掉這個(gè)碼,。同時(shí)設(shè)為30ms可以過濾掉一直按住一個(gè)鍵的情況。
|