예제 #1
0
 public GlobalKeyboardHookEventArgs(
     Win32.LowLevelKeyboardInputEvent keyboardData,
     Win32.KeyboardState keyboardState)
 {
     KeyboardData  = keyboardData;
     KeyboardState = keyboardState;
 }
예제 #2
0
        public IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool fEatKeyStroke = false;
            //
            var wparamTyped = wParam.ToInt32();

            if (Enum.IsDefined(typeof(Win32.KeyboardState), wparamTyped))
            {
                object o = Marshal.PtrToStructure(lParam, typeof(Win32.LowLevelKeyboardInputEvent));
                Win32.LowLevelKeyboardInputEvent p = (Win32.LowLevelKeyboardInputEvent)o;
                if (isDebug)
                {
                    System.Diagnostics.Debug.WriteLine($"system - p.Key {p.Key} - {((Win32.KeyboardState)wparamTyped)}");
                }

                bool isKeyStrlOrShift = p.Key == Keys.LShiftKey || p.Key == Keys.RShiftKey ||
                                        p.Key == Keys.LControlKey || p.Key == Keys.RControlKey;
                if (isKeyStrlOrShift)
                {
                    if (p.Key == Keys.LControlKey || p.Key == Keys.RControlKey)
                    {
                        ctrlPressed = save || ((Win32.KeyboardState)wparamTyped) == Win32.KeyboardState.KeyDown ||
                                      ((Win32.KeyboardState)wparamTyped) == Win32.KeyboardState.SysKeyDown;
                    }
                    if (p.Key == Keys.LShiftKey || p.Key == Keys.RShiftKey)
                    {
                        shiftPressed = save ||
                                       ((Win32.KeyboardState)wparamTyped) == Win32.KeyboardState.KeyDown ||
                                       ((Win32.KeyboardState)wparamTyped) == Win32.KeyboardState.SysKeyDown;
                    }

                    if (((Win32.KeyboardState)wparamTyped) == Win32.KeyboardState.KeyUp ||
                        ((Win32.KeyboardState)wparamTyped) == Win32.KeyboardState.SysKeyUp)
                    {
                        reset = true;
                    }
                }
                else
                {
                    save = ((Win32.KeyboardState)wparamTyped) == Win32.KeyboardState.KeyDown ||
                           ((Win32.KeyboardState)wparamTyped) == Win32.KeyboardState.SysKeyDown;
                }

                var eventArguments = new GlobalKeyboardHookEventArgs(p, (Win32.KeyboardState)wparamTyped);
                eventArguments.CtrlPressed  = ctrlPressed;
                eventArguments.ShiftPressed = shiftPressed;

                if (p.Key == HotKey)
                {
                    if ((eventArguments.KeyboardState == Win32.KeyboardState.KeyUp ||
                         eventArguments.KeyboardState == Win32.KeyboardState.SysKeyUp))
                    {
                        EventHandler <GlobalKeyboardHookEventArgs> handler = KeyboardPressed;
                        handler?.Invoke(this, eventArguments);
                    }
                    return((IntPtr)1);
                }

                if ((eventArguments.KeyboardState == Win32.KeyboardState.KeyUp ||
                     eventArguments.KeyboardState == Win32.KeyboardState.SysKeyUp) &&
                    !isKeyStrlOrShift)
                {
                    EventHandler <GlobalKeyboardHookEventArgs> handler = KeyboardPressed;
                    handler?.Invoke(this, eventArguments);

                    fEatKeyStroke = eventArguments.Handled;
                    save          = false;
                    if (reset)
                    {
                        ctrlPressed  = false;
                        shiftPressed = false;
                        reset        = false;
                    }
                }
            }
            else
            {
                System.Diagnostics.Debug.WriteLine($"!!!!!!!!!!!!!!!! wParam - {wParam}");
            }

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