public static int Hook_Callback(int code, int wParam, ref keyboardHookStruct lParam)
        {
            if (code < 0)
            {
                return(CallNextHookEx(Hook, code, wParam, ref lParam));
            }

            var  key       = (Key)System.Windows.Input.KeyInterop.KeyFromVirtualKey(lParam.vkCode);
            bool IsPressed = (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN);

            SetModifiers(key, IsPressed, lParam.vkCode);

            if (!HookedKeys.Contains(key))
            {
                return(CallNextHookEx(Hook, code, wParam, ref lParam));
            }

            KeyHookEventArgs kea = CreateEventArgs(key);

            var Handled = false;

            Handled = IsPressed ? TriggerKeyDown(kea) : TriggerKeyUp(kea);

            return(Handled ? 1 : CallNextHookEx(Hook, code, wParam, ref lParam));
        }
        public int KeyboardHookProc(int code, int wParam, IntPtr _lParam)
        {
            if (code >= 0)
            {
                var lParam = Marshal.PtrToStructure <KeyboardHookStruct>(_lParam);

                Keys key = (Keys)lParam.vkCode;
                if (HookedKeys.Contains(key))
                {
                    KeyEventArgs args = new KeyEventArgs(key);
                    if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && KeyDown != null)
                    {
                        KeyDown(this, args);
                    }
                    else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && KeyUp != null)
                    {
                        KeyUp(this, args);
                    }
                    if (args.Handled)
                    {
                        return(1);
                    }
                }
            }

            return(CallNextHookEx(_kbhook, code, wParam, _lParam));
        }
예제 #3
0
        public static Int32 Hook_Callback(Int32 code, Int32 wParam, ref keyboardHookStruct lParam)
        {
            if (code >= 0)
            {
                Key     key       = (Key)System.Windows.Input.KeyInterop.KeyFromVirtualKey(lParam.vkCode);
                Boolean IsPressed = (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN);

                SetModifiers(key, IsPressed, lParam.vkCode);
                if (HookedKeys.Contains(key))
                {
                    KeyHookEventArgs kea = CreateEventArgs(key);

                    Boolean Handled = false;
                    if (IsPressed)
                    {
                        Handled = TriggerKeyDown(kea);
                    }
                    else
                    {
                        Handled = TriggerKeyUp(kea);
                    }

                    if (Handled)
                    {
                        return(1);
                    }
                }
            }
            return(CallNextHookEx(Hook, code, wParam, ref lParam));
        }
        private bool KeyDispatch(Keys key, bool down)
        {
            if (!HookedKeys.Contains(key))
            {
                return(false);
            }

            KeyEventArgs eventArgs = new KeyEventArgs(key);

            if (down)
            {
                KeyDown?.Invoke(this, eventArgs);
            }
            else
            {
                KeyUp?.Invoke(this, eventArgs);
            }

            return(eventArgs.Handled);
        }