当前位置:编程学习 > C#/ASP.NET >>

C#键盘钩子问题

最近用网上写的C#键盘钩子获取USB扫描枪条码的一段代码遇到比较头疼的问题,扫描的条码值不对
扫描条码“KK25F1840W/019203001156”,用此代码结果是“kkk255F18400w/019203001156”
代码如下:(求高手解答)
BarCodeHook类:

using System.Runtime.InteropServices;
using System.Reflection;  


namespace WindowsApplication2
{
    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;    //扫描时间  
        }

        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;
        string strBarCode = "";

        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(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);
                        
                    }

                    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;
                }
            } 
            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)
            {
                return UnhookWindowsHookEx(hKeyboardHook);
            }
            return true;
        }

    }
}


页面代码:
 BarCodeHook BarCode = new BarCodeHook();  
        public Form2()
        {
            InitializeComponent();
            BarCode.BarCodeEvent += new BarCodeHook.BarCodeDelegate(BarCode_BarCodeEvent);  
        }
        private delegate void ShowInfoDelegate(BarCodeHook.BarCodes barCode);
        private void ShowInfo(BarCodeHook.BarCodes barCode)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new ShowInfoDelegate(ShowInfo), new object[] { 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;
            }
        }

        void BarCode_BarCodeEvent(BarCodeHook.BarCodes barCode)
        {
            ShowInfo(barCode);
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            BarCode.Start();  
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            BarCode.Stop();  
        }
 




--------------------编程问答-------------------- 条码值KK25F1840W/019203001156是哪几个参数? --------------------编程问答-------------------- ToUpper方法 --------------------编程问答-------------------- 如果只是大小写问题可以
string s = "kkk255F18400w/019203001156".ToUpper(); --------------------编程问答-------------------- 楼主说多个K? --------------------编程问答-------------------- 是的,有时出现讲1解析成!,将2解析成@…… --------------------编程问答-------------------- 找到问题原因了  谢谢各位@
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,