Beispiel #1
0
        protected HookProc m_KbdHookProc;               // 鍵盤掛鉤函式指標

        protected void OnHookInvoked(KeyboardHookEventArgs args)
        {
            if (m_HookEvent != null)
            {
                m_HookEvent(this, args);
            }
        }
Beispiel #2
0
        protected int KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                return(WinKernel.CallNextHookEx(m_HookHandle, nCode, wParam, lParam));
            }

            // 當按鍵按下及鬆開時都會觸發此函式,故先判斷是按下還是鬆開。
            bool isPressed = (lParam.ToInt32() & 0x80000000) == 0;
            Keys key       = (Keys)wParam.ToInt32();

            KeyboardHookEventArgs args = new KeyboardHookEventArgs(nCode, wParam.ToInt32(), lParam.ToInt32(), key, isPressed);

            OnHookInvoked(args);

            if (args.IsHandled)
            {
                return(1);
            }
            return(WinKernel.CallNextHookEx(m_HookHandle, nCode, wParam, lParam));
        }