WIN CE 键盘钩子 hHookKey 值一直为 0

.Net技术 码拜 10年前 (2014-12-22) 1753次浏览 0个评论

网上下载的代码, 运行一直不成功   hHookKey 值一直为 0
CE5.0 + .NET 3.5 的环境
请高手 帮忙看下  已经搞了二天了

  public class Hook
    {
        int hHookKey;
        public delegate int HookKeyProc(int code, IntPtr wParam, IntPtr lParam);
        private HookKeyProc hookKeyDeleg;
        public Hook()
        { }
        #region public methods
        //安装钩子
        public void Start()
        {
            if (hHookKey != 0)
            {
               //Unhook the previouse one
                this.Stop();
            }
            hookKeyDeleg = new HookKeyProc(HookKeyProcedure);
            // hHookKey = SetWindowsHookEx(WH_KEYBOARD_LL, hookKeyDeleg, GetModuleHandle(null), 0);
            hHookKey = SetWindowsHookEx(WH_KEYBOARD_LL, hookKeyDeleg, GetModuleHandle(null), 0);
            if (hHookKey == 0)
            {
                throw new SystemException("Failed acquiring of the hook.");
            }
        }
        //拆除钩子
        public void Stop()
        {
            UnhookWindowsHookEx(hHookKey);
        }
        #endregion
        #region protected and private methods
        private int HookKeyProcedure(int code, IntPtr wParam, IntPtr lParam)
        {
            KBDLLHOOKSTRUCT hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
            if (code < 0)
                return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
            if (hookStruct.vkCode == 0x5B)
            {
                //如果捕捉到VK_LWIN按键 
                //......处理......             
                return -1; //返回-1表示已经处理了,不再往下传递
            }
            if (hookStruct.vkCode == 0x25)
            {
                //如果捕捉到VK_LEFT按键 
                //......处理......
                return -1;
            }
            // 没处理的键的消息往下传递
            return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
        }
        #endregion
        #region P/Invoke declarations
        [DllImport("coredll.dll")]
        private static extern int SetWindowsHookEx(int type, HookKeyProc HookKeyProc, IntPtr hInstance, int m);
        //private static extern int SetWindowsHookEx(int type, HookMouseProc HookMouseProc, IntPtr hInstance, int m);
        //[DllImport("coredll.dll")]
        //public static extern IntPtr GetModuleHandle(string name); 
        [DllImport("coredll.dll")]
        private static extern IntPtr GetModuleHandle(string mod);
        [DllImport("coredll.dll")]
        private static extern int CallNextHookEx(
                HookKeyProc hhk,
                int nCode,
                IntPtr wParam,
                IntPtr lParam
                );
        [DllImport("coredll.dll")]
        private static extern int GetCurrentThreadId();
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern int UnhookWindowsHookEx(int idHook);
        private struct KBDLLHOOKSTRUCT
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public IntPtr dwExtraInfo;
        }
        const int WH_KEYBOARD_LL = 20;
        public class KeyBoardInfo
        {
            public int vkCode;
            public int scanCode;
            public int flags;

            public int time;
        }
        #endregion
    }

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明WIN CE 键盘钩子 hHookKey 值一直为 0
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!