Пример #1
0
        public IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool fEatKeyStroke = false;

            var wparamTyped = wParam.ToInt32();

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

                var eventArguments = new GlobalKeyboardHookEventArgs(p, (KeyboardState)wparamTyped);

                // EDT: Removed the comparison-logic from the usage-area so the user does not need to mess around with it.
                // Either the incoming key has to be part of RegisteredKeys (see constructor on top) or RegisterdKeys
                // has to be null for the event to get fired.
                var key = (Keys)p.VirtualCode;
                if (RegisteredKeys == null || RegisteredKeys.Contains(key))
                {
                    EventHandler <GlobalKeyboardHookEventArgs> handler = KeyboardPressed;
                    handler?.Invoke(this, eventArguments);

                    fEatKeyStroke = eventArguments.Handled;
                }
            }

            return(fEatKeyStroke ? (IntPtr)1 : CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam));
        }
 internal GlobalKeyboardHookEventArgs(
     LowLevelKeyboardInputEvent keyboardData,
     GlobalKeyboardHook.KeyboardState keyboardState)
 {
     KeyboardData  = keyboardData;
     KeyboardState = keyboardState;
 }
Пример #3
0
        public IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool fEatKeyStroke = false;

            var wparamTyped = wParam.ToInt32();

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

                var eventArguments = new GlobalKeyboardHookEventArgs(p, (KeyboardState)wparamTyped);

                var key = (KeyForm)p.VirtualCode;
                if (RegisteredKeys == null || RegisteredKeys.Contains(key))
                {
                    EventHandler <GlobalKeyboardHookEventArgs> handler = KeyboardPressed;
                    handler?.Invoke(this, eventArguments);

                    fEatKeyStroke = eventArguments.Handled;
                }
            }

            return(fEatKeyStroke ? (IntPtr)1 : CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam));
        }
Пример #4
0
        public void KeyEvent()
        {
            TrayBasedContext tbc = new TrayBasedContext(null, new KeyPressState(), new JsonConfigProvider(localConfig));
            var llie             = new LowLevelKeyboardInputEvent();
            var g = new GlobalKeyboardHookEventArgs(llie, KeyboardState.KeyUp);


            tbc.HandleKey(null, g);
        }
Пример #5
0
        public IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool fEatKeyStroke = false;

            var wparamTyped = wParam.ToInt32();

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

                var eventArguments = new KeyboardHookEventArgs(p, (Native.KeyboardState)wparamTyped);

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

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

                var altdown  = ((flags) & ((int)KeyFlags.KF_ALTDOWN >> 8)) > 0;
                var dlgmode  = ((flags) & ((int)KeyFlags.KF_DLGMODE >> 8)) > 0;
                var menumode = ((flags) & ((int)KeyFlags.KF_MENUMODE >> 8)) > 0;
                var repeat   = ((flags) & ((int)KeyFlags.KF_REPEAT >> 8)) > 0;
                var up       = ((flags) & ((int)KeyFlags.KF_UP >> 8)) > 0;


                //TODO: why is this code ignoring virtual keys and mapping it custom?
                //TODO: figure out what extended 2 is supposed to do from the raw input opentk code....
                var is_valid = true;
                Key key      = WinKeyMap.TranslateKey(scancode, vkey, extended, false, out is_valid);



                if (is_valid)
                {
                    if (eventArguments.KeyboardState == Native.KeyboardState.KeyDown || eventArguments.KeyboardState == Native.KeyboardState.SysKeyDown)
                    {
                        OnKeyDown(key);
                        fEatKeyStroke = KeyDownArgs.Handled;
                    }
                    if (eventArguments.KeyboardState == Native.KeyboardState.KeyUp || eventArguments.KeyboardState == Native.KeyboardState.SysKeyUp)
                    {
                        OnKeyUp(key);
                        fEatKeyStroke = KeyUpArgs.Handled;
                    }
                }
            }

            return(fEatKeyStroke ? (IntPtr)1 : NativeMethods.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam));
        }
Пример #6
0
        public IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool fEatKeyStroke = false;
            var  wParamTyped   = wParam.ToInt32();

            if (Enum.IsDefined(typeof(KeyboardState), wParamTyped))
            {
                LowLevelKeyboardInputEvent p = (LowLevelKeyboardInputEvent)Marshal.PtrToStructure(lParam, typeof(LowLevelKeyboardInputEvent));
                var eventArguments           = new GlobalKeyboardListenerEventArgs(p, (KeyboardState)wParamTyped);
                EventHandler <GlobalKeyboardListenerEventArgs> handler = KeyboardPressed;

                CheckKeys(eventArguments);
                handler?.Invoke(this, eventArguments);

                fEatKeyStroke = eventArguments.Handled;
            }

            return(fEatKeyStroke ? (IntPtr)1 : SystemDll.User32.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam));
        }
Пример #7
0
        public IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool fEatKeyStroke = false;

            var wparamTyped = wParam.ToInt32();
            if (Enum.IsDefined(typeof(KeyboardState), wparamTyped))
            {
                object o = Marshal.PtrToStructure(lParam, typeof(LowLevelKeyboardInputEvent));
                LowLevelKeyboardInputEvent p = (LowLevelKeyboardInputEvent)o;

                var eventArguments = new GlobalKeyboardHookEventArgs(p, (KeyboardState)wparamTyped);

                EventHandler<GlobalKeyboardHookEventArgs> handler = KeyboardPressed;
                handler?.Invoke(this, eventArguments);

                fEatKeyStroke = eventArguments.Handled;
            }

            return fEatKeyStroke ? (IntPtr)(-1) : DLLImports.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
        }
Пример #8
0
        // called by WIN on Keys
        public IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool fEatKeyStroke = false;

            var wparamTyped = wParam.ToInt32();

            if (Enum.IsDefined(typeof(KeyboardState), wparamTyped))
            {
                object o = Marshal.PtrToStructure(lParam, typeof(LowLevelKeyboardInputEvent));
                LowLevelKeyboardInputEvent p = (LowLevelKeyboardInputEvent)o;
                KeyboardState kbdState       = (KeyboardState)wparamTyped;
                bool          kDown          = kbdState == KeyboardState.KeyDown || kbdState == KeyboardState.SysKeyDown;
                var           eventArguments = new GlobalKbdHookEventArgs(p, kbdState, kDown);
                EventHandler <GlobalKbdHookEventArgs> handler = KeyboardPressed;
                handler?.Invoke(this, eventArguments);

                fEatKeyStroke = eventArguments.Handled;
            }
            // propagates or closes the event (based on Handled)
            return(fEatKeyStroke ? (IntPtr)1 : CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam));
        }
Пример #9
0
        // 该回调函数负责处理键盘事件
        // 参考:http://tech.sina.com.cn/s/2006-07-19/09091044360.shtml
        public IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool fEatKeyStroke = false;

            var wparamTyped = wParam.ToInt32();

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

                var eventArguments = new GlobalKeyboardHookEventArgs(p, (KeyboardState)wparamTyped);
                // 不要在事件处理放入耗时长的操作,会导致阻断失效。
                var handler = KeyboardEvent;
                handler?.Invoke(this, eventArguments);
                fEatKeyStroke = eventArguments.Handled;
            }
            // 如果 Handled 为真,那么会返回一个哑值 1。
            return(fEatKeyStroke ? (IntPtr)1 : CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam));
            //return (IntPtr)1;
        }
Пример #10
0
        private IntPtr LowLevelKeyboardProc(int nCode,
                                            IntPtr wParam,
                                            IntPtr lParam)
        {
            if (nCode < 0)
            {
                return(CallNextHookEx(_windowsHookHandle,
                                      nCode,
                                      wParam,
                                      lParam));
            }

            bool fEatKeyStroke = false;

            var wparamTyped = wParam.ToInt32();

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

                var eArgs = new KeyboardHookEventArgs(p,
                                                      (KeyboardState)wparamTyped,
                                                      GetCtrlPressed(),
                                                      GetAltPressed(),
                                                      GetShiftPressed(),
                                                      GetMetaPressed());

                //EventHandler<KeyboardHookEventArgs> handler = KeyboardPressed;
                //handler?.Invoke(this,
                //eArgs);

                fEatKeyStroke = eArgs.Handled;

                if (fEatKeyStroke == false && eArgs.KeyboardState == KeyboardState.KeyDown || eArgs.KeyboardState == KeyboardState.SysKeyDown)
                {
                    var func = HotKeys.SafeGet(new HotKey(eArgs.Ctrl,
                                                          eArgs.Alt,
                                                          eArgs.Shift,
                                                          eArgs.Meta,
                                                          eArgs.KeyboardData.Key,
                                                          null)
                                               );

                    if (func != null)
                    {
                        Task.Factory.StartNew(() => func.Invoke());
                        fEatKeyStroke = true;
                    }
                }
            }

            return(fEatKeyStroke
        ? (IntPtr)1
        : CallNextHookEx(_windowsHookHandle,
                         nCode,
                         wParam,
                         lParam));
        }
Пример #11
0
 public GlobalKeyboardHookEventArgs(LowLevelKeyboardInputEvent keyboardData, KeyboardState keyboardState)
 {
     KeyboardData  = keyboardData;
     KeyboardState = keyboardState;
 }