Example #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);

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

                fEatKeyStroke = eventArguments.Handled;
            }

            return(fEatKeyStroke ? (IntPtr)1 : CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam));
        }
Example #2
0
        private void Handle(GlobalKeyboardHookEventArgs e)
        {
            var key      = (Keys)e.KeyboardData.VirtualCode;
            var modifier = ToModifier(key);

            var tuple = Tuple.Create(pressedModifiers, key);

            if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown && key != lastKey)
            {
                lastKey           = key;
                pressedModifiers |= modifier;

                if (registered.ContainsKey(tuple))
                {
                    registered[tuple]();
                }
            }
            else if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyUp)
            {
                lastKey           = Keys.None;
                pressedModifiers ^= modifier;
            }
        }
Example #3
0
 private void OnKeyPressed(object sender, GlobalKeyboardHookEventArgs e)
 {
     dispatch.Invoke((MethodInvoker) delegate { Handle(e); });
 }