问题描述
最近用网上写的C#键盘钩子获取USB扫描枪条码的一段代码遇到比较头疼的问题,扫描的条码值不对扫描条码“KK25F1840W/019203001156”,用此代码结果是“kkk255F18400w/019203001156”代码如下:(求高手解答)BarCodeHook类:usingSystem.Runtime.InteropServices;usingSystem.Reflection;namespaceWindowsApplication2{publicclassBarCodeHook{publicdelegatevoidBarCodeDelegate(BarCodesbarCode);publiceventBarCodeDelegateBarCodeEvent;publicstructBarCodes{publicintVirtKey;//虚拟码publicintScanCode;//扫描码publicstringKeyName;//键名publicuintAscII;//AscIIpubliccharChr;//字符publicstringBarCode;//条码信息publicboolIsValid;//条码是否有效publicDateTimeTime;//扫描时间}privatestructEventMsg{publicintmessage;publicintparamL;publicintparamH;publicintTime;publicinthwnd;}[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]privatestaticexternintSetWindowsHookEx(intidHook,HookProclpfn,IntPtrhInstance,intthreadId);[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]privatestaticexternboolUnhookWindowsHookEx(intidHook);[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]privatestaticexternintCallNextHookEx(intidHook,intnCode,Int32wParam,IntPtrlParam);[DllImport("user32",EntryPoint="GetKeyNameText")]privatestaticexternintGetKeyNameText(intlParam,StringBuilderlpBuffer,intnSize);[DllImport("user32",EntryPoint="GetKeyboardState")]privatestaticexternintGetKeyboardState(byte[]pbKeyState);[DllImport("user32",EntryPoint="ToAscii")]privatestaticexternboolToAscii(intVirtualKey,intScanCode,byte[]lpKeyState,refuintlpChar,intuFlags);delegateintHookProc(intnCode,Int32wParam,IntPtrlParam);BarCodesbarCode=newBarCodes();inthKeyboardHook=0;stringstrBarCode="";privateintKeyboardHookProc(intnCode,Int32wParam,IntPtrlParam){if(nCode==0){EventMsgmsg=(EventMsg)Marshal.PtrToStructure(lParam,typeof(EventMsg));if(wParam==0x100)//WM_KEYDOWN=0x100{barCode.VirtKey=msg.message&0xff;//虚拟码barCode.ScanCode=msg.paramL&0xff;//扫描码StringBuilderstrKeyName=newStringBuilder(255);if(GetKeyNameText(barCode.ScanCode*65536,strKeyName,255)>0){barCode.KeyName=strKeyName.ToString().Trim(newchar[]{'',''});}else{barCode.KeyName="";}byte[]kbArray=newbyte[256];uintuKey=0;GetKeyboardState(kbArray);if(ToAscii(barCode.VirtKey,barCode.ScanCode,kbArray,refuKey,0)){barCode.AscII=uKey;barCode.Chr=Convert.ToChar(uKey);}if(DateTime.Now.Subtract(barCode.Time).TotalMilliseconds>50){strBarCode=barCode.Chr.ToString();}else{if((msg.message&0xff)==13&&strBarCode.Length>3)//回车{barCode.BarCode=strBarCode;barCode.IsValid=true;strBarCode="";}strBarCode+=barCode.Chr.ToString();}barCode.Time=DateTime.Now;if(BarCodeEvent!=null)BarCodeEvent(barCode);//触发事件barCode.IsValid=false;}}returnCallNextHookEx(hKeyboardHook,nCode,wParam,lParam);}//安装钩子publicboolStart(){if(hKeyboardHook==0){//WH_KEYBOARD_LL=13hKeyboardHook=SetWindowsHookEx(13,newHookProc(KeyboardHookProc),Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);}return(hKeyboardHook!=0);}//卸载钩子publicboolStop(){if(hKeyboardHook!=0){returnUnhookWindowsHookEx(hKeyboardHook);}returntrue;}}}页面代码:BarCodeHookBarCode=newBarCodeHook();publicForm2(){InitializeComponent();BarCode.BarCodeEvent+=newBarCodeHook.BarCodeDelegate(BarCode_BarCodeEvent);}privatedelegatevoidShowInfoDelegate(BarCodeHook.BarCodesbarCode);privatevoidShowInfo(BarCodeHook.BarCodesbarCode){if(this.InvokeRequired){this.BeginInvoke(newShowInfoDelegate(ShowInfo),newobject[]{barCode});}else{textBox1.Text=barCode.KeyName;textBox2.Text=barCode.VirtKey.ToString();textBox3.Text=barCode.ScanCode.ToString();textBox4.Text=barCode.AscII.ToString();textBox5.Text=barCode.Chr.ToString();//textBox6.Text=barCode.IsValid?barCode.BarCode:"";textBox6.Text=barCode.BarCode;}}voidBarCode_BarCodeEvent(BarCodeHook.BarCodesbarCode){ShowInfo(barCode);}privatevoidForm2_Load(objectsender,EventArgse){BarCode.Start();}privatevoidForm2_FormClosed(objectsender,FormClosedEventArgse){BarCode.Stop();}
解决方案
解决方案二:
条码值KK25F1840W/019203001156是哪几个参数?
解决方案三:
ToUpper方法
解决方案四:
如果只是大小写问题可以strings="kkk255F18400w/019203001156".ToUpper();
解决方案五:
楼主说多个K?
解决方案六:
是的,有时出现讲1解析成!,将2解析成@……
解决方案七:
找到问题原因了谢谢各位@
解决方案八:
楼主,怎么解决的啊?能否分享一下呢?
解决方案:
如果可以QQ联系2535052261
解决方案:
我给你的建议是,你扫条码,首先条码都是有规则的,比如前几位是什么,日期码是哪几位,校验规则是什么。这样你在扫条码的时候,比如你这种条码开头是KK的话,你钩子接受到KK,就加到list<char>然后继续接收,不是那就清空。然后条码是多少位,最后一定有个算法校验的。判断出你接收的是正确的条码你再做后续操作。当年做条码扫描也是吃了不少苦头啊。。。