Пример #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));
        }
Пример #2
0
        private void KeyPressed(object sender, GlobalKeyboardHookEventArgs e)
        {
            var pressedKey = (Keys)e.KeyboardData.VirtualCode;

            if (pressedKey == Keys.LShiftKey)
            {
                _shiftPressed = e.KeyboardState == KeyboardState.KeyDown;
                return;
            }
            if (e.KeyboardState != KeyboardState.KeyDown)
            {
                return;
            }
            char resultSymbol;

            if (pressedKey.TryToChar(_shiftPressed, out resultSymbol))
            {
                AppendBuffer(resultSymbol, Strategy);
                NewSymbol?.Invoke(this, new NewSymbolEventArgs(resultSymbol));
            }
            CheckBuffer(Strategy, pressedKey);
        }