Esempio n. 1
0
        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
                {
                    barCode.VirtKey  = msg.message & 0xff; //虚拟吗
                    barCode.ScanCode = msg.paramL & 0xff;  //扫描码
                    StringBuilder strKeyName = new StringBuilder(225);
                    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.Ascll = uKey;
                        barCode.Chr   = Convert.ToChar(uKey);
                    }

                    TimeSpan ts = DateTime.Now.Subtract(barCode.Time);

                    if (ts.TotalMilliseconds > 50)
                    {
                        //时间戳,大于50 毫秒表示手动输入
                        //strBarCode = barCode.Chr.ToString();
                        sbBarCode.Remove(0, sbBarCode.Length);
                        sbBarCode.Append(barCode.Chr.ToString());
                        barCode.OriginalChrs    = " " + Convert.ToString(barCode.Chr);
                        barCode.OriginalAsciis  = " " + Convert.ToString(barCode.Ascll);
                        barCode.OriginalBarCode = Convert.ToString(barCode.Chr);
                    }
                    else
                    {
                        sbBarCode.Append(barCode.Chr.ToString());
                        if ((msg.message & 0xff) == 13 && sbBarCode.Length > 1)
                        {                                           //回车
                         //barCode.BarCode = strBarCode;
                            barCode.BarCode = sbBarCode.ToString(); // barCode.OriginalBarCode;
                            barCode.IsValid = true;
                            sbBarCode.Remove(0, sbBarCode.Length);
                        }
                        //strBarCode += barCode.Chr.ToString();
                    }
                    barCode.Time = DateTime.Now;
                    try
                    {
                        if (BarCodeEvent != null && barCode.IsValid)
                        {
                            AsyncCallback callback = new AsyncCallback(AsyncBack);
                            //object obj;
                            Delegate[] delArray = BarCodeEvent.GetInvocationList();
                            //foreach (Delegate del in delArray)
                            foreach (BardCodeDeletegate del in delArray)
                            {
                                try
                                {
                                    //方法1
                                    //obj = del.DynamicInvoke(barCode);
                                    //方法2
                                    del.BeginInvoke(barCode, callback, del);//异步调用防止界面卡死
                                }
                                catch (Exception ex)
                                {
                                    throw ex;
                                }
                            }
                            //BarCodeEvent(barCode);//触发事件
                            barCode.BarCode         = "";
                            barCode.OriginalChrs    = "";
                            barCode.OriginalAsciis  = "";
                            barCode.OriginalBarCode = "";
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        barCode.IsValid = false; //最后一定要 设置barCode无效
                        barCode.Time    = DateTime.Now;
                    }
                }
            }
            return(CallNextHookEx(hKeyboardHook, nCode, wParam, lParam));
        }
Esempio n. 2
0
        /// <summary>
        /// 键盘钩子处理函数
        /// </summary>
        private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode != 0)
            {
                return(CallNextHookEx(_hKeyboardHook, nCode, wParam, lParam));
            }
            var msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof(EventMsg));

            if (wParam != 0x100)
            {
                //0x100标识键盘按下
                return(CallNextHookEx(_hKeyboardHook, nCode, wParam, lParam));
            }
            _barCode.VirtKey  = msg.message & 0xff; //虚拟吗
            _barCode.ScanCode = msg.paramL & 0xff;  //扫描码
            var strKeyName = new StringBuilder(225);

            _barCode.KeyName = GetKeyNameText(_barCode.ScanCode * 65536, strKeyName, 255) > 0 ? strKeyName.ToString().Trim(' ', '\0') : "";
            var  kbArray = new byte[256];
            uint uKey    = 0;

            GetKeyboardState(kbArray);
            if (ToAscii(_barCode.VirtKey, _barCode.ScanCode, kbArray, ref uKey, 0))
            {
                _barCode.Ascll = uKey;
                _barCode.Chr   = Convert.ToChar(uKey);
            }
            var ts = DateTime.Now.Subtract(_barCode.Time);

            if (ts.TotalMilliseconds > 50)
            {
                //时间戳,大于50 毫秒表示手动输入
                SbBarCode.Remove(0, SbBarCode.Length);
                SbBarCode.Append(_barCode.Chr.ToString());
                _barCode.OriginalChrs    = " " + Convert.ToString(_barCode.Chr);
                _barCode.OriginalAsciis  = " " + Convert.ToString(_barCode.Ascll);
                _barCode.OriginalBarCode = Convert.ToString(_barCode.Chr);
            }
            else
            {
                SbBarCode.Append(_barCode.Chr.ToString());
                if ((msg.message & 0xff) == 13 && SbBarCode.Length > 3)
                {
                    //回车
                    _barCode.BarCode = SbBarCode.Replace("\r", "").ToString();
                    _barCode.IsValid = true;
                    SbBarCode.Remove(0, SbBarCode.Length);
                }
            }
            _barCode.Time = DateTime.Now;
            try
            {
                if (BarCodeEvent != null && _barCode.IsValid)
                {
                    var callback = new AsyncCallback(AsyncBack);
                    var delArray = BarCodeEvent.GetInvocationList();
                    foreach (var del in delArray)
                    {
                        var barcodeDelegate = del as BardCodeDeletegate;
                        //异步调用防止界面卡死
                        barcodeDelegate?.BeginInvoke(_barCode, callback, del);
                    }
                    _barCode.BarCode         = "";
                    _barCode.OriginalChrs    = "";
                    _barCode.OriginalAsciis  = "";
                    _barCode.OriginalBarCode = "";
                }
            }
            finally
            {
                //最后一定要 设置barCode无效
                _barCode.IsValid = false;
                _barCode.Time    = DateTime.Now;
            }
            return(CallNextHookEx(_hKeyboardHook, nCode, wParam, lParam));
        }