public KeyboardHookEventArgs(
     LowLevelKeyboardInputEvent keyboardData,
     WindowsKeyboardState keyboardState)
 {
     KeyboardData  = keyboardData;
     KeyboardState = keyboardState;
 }
Ejemplo n.º 2
0
        public IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool fEatKeyStroke = false;

            var wparamTyped = wParam.ToInt32();

            if (Enum.IsDefined(typeof(WindowsKeyboardState), wparamTyped))
            {
                object o = Marshal.PtrToStructure(lParam, typeof(LowLevelKeyboardInputEvent));

                LowLevelKeyboardInputEvent p = (LowLevelKeyboardInputEvent)o;

                var eventArguments = new KeyboardHookEventArgs(p, (WindowsKeyboardState)wparamTyped);

                var scancode = eventArguments.KeyboardData.HardwareScanCode;
                var vkey     = (VirtualKeys)eventArguments.KeyboardData.VirtualCode;

                var flags    = eventArguments.KeyboardData.Flags;
                var extended = ((flags) & ((int)WindowsKeyFlags.KF_EXTENDED >> 8)) > 0;

                var is_valid = true;
                Key key      = WinKeyMap.TranslateKey(scancode, vkey, extended, false, out is_valid);

                if (is_valid)
                {
                    if (eventArguments.KeyboardState == WindowsKeyboardState.KeyDown || eventArguments.KeyboardState == WindowsKeyboardState.SysKeyDown)
                    {
                        OnKeyDown(key);

                        fEatKeyStroke = KeyDownArgs.Handled;
                    }
                    if (eventArguments.KeyboardState == WindowsKeyboardState.KeyUp || eventArguments.KeyboardState == WindowsKeyboardState.SysKeyUp)
                    {
                        OnKeyUp(key);

                        fEatKeyStroke = KeyUpArgs.Handled;
                    }
                }
            }

            return(fEatKeyStroke ? (IntPtr)1 : NativeApi.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam));
        }