コード例 #1
0
        public bool UnInstallHook()
        {
            bool result = true;

            if (this.m_pKeyboardHook != IntPtr.Zero)
            {
                result = (APIWrapper.UnhookWindowsHookEx(this.m_pKeyboardHook) && result);
                this.m_pKeyboardHook = IntPtr.Zero;
            }
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// 键盘钩子处理函数
        /// </summary>
        /// <remarks>此版本的键盘事件处理不是很好,还有待修正.</remarks>
        private int LowLevelKeyboardProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            if (nCode == 0)
            {
                //获得信息
                KeyboardLLHookStruct KeyboardInfo =
                    (KeyboardLLHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardLLHookStruct));
                uint VKCode = KeyboardInfo.VKCode;

                if (this.OnKeyDown != null && (wParam == (int)enmKeyboardMessageType.WM_SYSKEYDOWN ||
                                               wParam == (int)enmKeyboardMessageType.WM_KEYDOWN))
                {
                    Keys         keyData  = (Keys)KeyboardInfo.VKCode;
                    KeyEventArgs keyEvent = new KeyEventArgs(keyData);
                    this.OnKeyDown(this, keyEvent);
                }

                if (this.OnKeyPress != null && wParam == (Int32)enmKeyboardMessageType.WM_KEYUP)
                {
                    byte[] inBuffer = new byte[2];

                    /*
                     * 当ToAscii返回1个字符表示为按键,
                     * 为0表示转换失败
                     * 为2表示转换了2个字符,在KeyPressEventArgs中只有一个Char信息,所以此中情况将忽略。
                     * 一般在特殊键盘输入(如德语、法语等的注音)时发生。
                     */
                    if (APIWrapper.ToAscii(KeyboardInfo.VKCode,
                                           KeyboardInfo.ScanCode,
                                           this.m_KeyState,
                                           inBuffer,
                                           KeyboardInfo.Flags) == 1)
                    {
                        KeyPressEventArgs keyPressEvent = new KeyPressEventArgs((char)inBuffer[0]);
                        this.OnKeyPress(this, keyPressEvent);
                    }
                }

                if (this.OnKeyUp != null && (wParam == (Int32)enmKeyboardMessageType.WM_KEYUP ||
                                             wParam == (Int32)enmKeyboardMessageType.WM_SYSKEYUP))
                {
                    Keys         keyData  = (Keys)KeyboardInfo.VKCode;
                    KeyEventArgs keyEvent = new KeyEventArgs(keyData);
                    this.OnKeyUp(this, keyEvent);
                }
            }

            return(APIWrapper.CallNextHookEx(this.m_pKeyboardHook, nCode, wParam, lParam));
        }
コード例 #3
0
        public bool InstallHook()
        {
            IntPtr pInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule);

            if (this.m_pKeyboardHook == IntPtr.Zero)
            {
                this.m_KeyboardHookProcedure = new HookProc(this.LowLevelKeyboardProc);
                this.m_pKeyboardHook         = APIWrapper.SetWindowsHookEx(enmHookType.WH_KEYBOARD_LL,
                                                                           this.m_KeyboardHookProcedure, pInstance, 0);
                if (this.m_pKeyboardHook == IntPtr.Zero)
                {
                    int nErrCode = Marshal.GetLastWin32Error();

                    this.UnInstallHook();
                    return(false);
                }
            }

            return(true);
        }
コード例 #4
0
 public Hook()
 {
     APIWrapper.GetKeyboardState(this.m_KeyState);
 }