private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { if (nCode == 0) { EventMsg msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof(EventMsg)); if (wParam == 0x100) //WM_KEYDOWN=0x100 { _char.VirtKey = msg.message & 0xff; //虚拟吗 _char.ScanCode = msg.paramL & 0xff; //扫描码 if (_char.VirtKey == (int)Keys.LShiftKey) { goto END1; } StringBuilder strKeyName = new StringBuilder(225); if (GetKeyNameText(_char.ScanCode * 65536, strKeyName, 255) > 0) { _char.KeyName = strKeyName.ToString().Trim(new char[] { ' ', '\0' }); } else { _char.KeyName = ""; } byte[] kbArray = new byte[256]; uint uKey = 0; GetKeyboardState(kbArray); if (ToAscii(_char.VirtKey, _char.ScanCode, kbArray, ref uKey, 0)) { _char.Ascii = uKey; _char.Chr = Convert.ToChar(uKey); } TimeSpan ts = DateTime.Now.Subtract(_string.Time); // Debug.WriteLine($"ts={ts.TotalMilliseconds} char:'{_char.Chr.ToString()}' code:{(int)(_char.Chr)}"); Debug.WriteLine($"ts={ts.TotalMilliseconds} code:{(int)(_char.Chr)}"); if (_char.Chr == 0) { goto END1; } if (ts.TotalMilliseconds > TIME_SHTRESHOLD) { //时间戳,大于50 毫秒表示手动输入 //strBarCode = barCode.Chr.ToString(); _barcode.Remove(0, _barcode.Length); _barcode.Append(_char.Chr.ToString()); //_char.OriginalChrs = " " + Convert.ToString(_char.Chr); //_char.OriginalAsciis = " " + Convert.ToString(_char.Ascii); //_char.OriginalBarCode = Convert.ToString(_char.Chr); } else { _barcode.Append(_char.Chr.ToString()); Debug.WriteLine($"msg.message={(msg.message & 0xff)} _barcode:'{_barcode.ToString()}'"); if ((msg.message & 0xff) == 13 && _barcode.Length > 3) { // 回车 _string.Barcode = _barcode.ToString();// barCode.OriginalBarCode; _string.IsValid = true; _barcode.Remove(0, _barcode.Length); Debug.WriteLine($"isValid = true"); } } _string.Time = DateTime.Now; try { if (InputLine != null && _string.IsValid ) { Debug.WriteLine($"trigger callback"); AsyncCallback callback = new AsyncCallback(AsyncBack); Delegate[] delArray = InputLine.GetInvocationList(); foreach (Delegate_lineFeed del in delArray) { try { del.BeginInvoke(_string, callback, del); // 异步调用防止界面卡死 } catch (Exception ex) { throw ex; } } //BarCodeEvent(barCode);//触发事件 _string.Barcode = ""; //_char.OriginalChrs = ""; //_char.OriginalAsciis = ""; //_char.OriginalBarCode = ""; } } catch (Exception) { throw; } finally { _string.IsValid = false; _string.Time = DateTime.Now; } } } END1: return(CallNextHookEx(hKeyboardHook, nCode, wParam, lParam)); }
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { if (nCode == 0) { EventMsg msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof(EventMsg)); if (wParam == WM_KEYUP) { int vkCode = Marshal.ReadInt32(lParam); Keys key = (Keys)vkCode; if (key == Keys.LShiftKey) { _shift = false; goto END1; } } if (wParam == WM_KEYDOWN)//WM_KEYDOWN=0x100 { int vkCode = Marshal.ReadInt32(lParam); Keys key = (Keys)vkCode; if (key == Keys.LShiftKey) { _shift = true; // Console.WriteLine($"modi {(System.Windows.Forms.Keys)vkCode}"); goto END1; } TimeSpan ts = DateTime.Now.Subtract(_string.Time); if (ts.TotalMilliseconds > TIME_SHTRESHOLD) { _barcode.Clear(); } _string.Time = DateTime.Now; //KeysConverter kc = new KeysConverter(); //string keyChar = kc.ConvertToString(key); string keyChar = GetKeyString(key, _shift); Debug.WriteLine($"keyChar='{keyChar}', key='{key.ToString()}'"); _barcode.Append(keyChar); // 保护缓冲区 if (_barcode.Length > 1000) { _barcode.Clear(); } Debug.WriteLine($"msg.message={(msg.message & 0xff)} _barcode:'{_barcode.ToString()}'"); if (InputChar != null) { CharInput input = new CharInput { Key = key, KeyChar = keyChar, }; AsyncCallback callback = new AsyncCallback(AsyncBackCharFeed); Delegate[] delArray = InputChar.GetInvocationList(); foreach (Delegate_charFeed del in delArray) { try { del.BeginInvoke(input, callback, del); } catch (Exception ex) { throw ex; } } } if ((msg.message & 0xff) == 13 && _barcode.Length > 3) { // 回车 _string.Barcode = _barcode.ToString();// barCode.OriginalBarCode; _string.IsValid = true; _barcode.Remove(0, _barcode.Length); Debug.WriteLine($"isValid = true"); } else { goto END1; } try { if (InputLine != null // && _string.IsValid ) { Debug.WriteLine($"trigger callback"); AsyncCallback callback = new AsyncCallback(AsyncBackLineFeed); Delegate[] delArray = InputLine.GetInvocationList(); foreach (Delegate_lineFeed del in delArray) { try { del.BeginInvoke(_string, callback, del); // 异步调用防止界面卡死 } catch (Exception ex) { throw ex; } } //BarCodeEvent(barCode);//触发事件 _string.Barcode = ""; //_char.OriginalChrs = ""; //_char.OriginalAsciis = ""; //_char.OriginalBarCode = ""; } } catch (Exception) { throw; } finally { _string.IsValid = false; _string.Time = DateTime.Now; } } } END1: if (Handled == true) { return(1); } return(CallNextHookEx(hKeyboardHook, nCode, wParam, lParam)); }