Exemplo n.º 1
0
        private void EventHook_eventKey(object sender, GlobalKeyboardHookEventArgs e)
        {
            logger?.Trace($"key - {e.KeyboardData.Key}, shift -  {e.ShiftPressed}, ctr - {e.CtrlPressed}, state - {e.KeyboardState}");

            if (e.KeyboardData.Key == switchKey)
            {
                ChangeLanguage();
                return;
            }
            if (e.KeyboardData.Key == Keys.Back)
            {
                if (words.Count > 0)
                {
                    var ch = words[words.Count - 1];
                    words.RemoveAt(words.Count - 1);
                    logger?.Trace($"Remove last char - '{ch}' from words, now length - {words.Count}");
                }
                else
                {
                    logger?.Trace($"words is empty");
                }
                return;
            }
            var keyString = !e.CtrlPressed ? KeysConv.ConvertToString(e.KeyboardData.Key) : null;

            logger?.Trace($"keyString - {keyString}");
            if (keyString != null)
            {
                words.Add(Tuple.Create(keyString, e.ShiftPressed));
                logger?.Trace($"now length words - {words.Count} - {string.Join(",", words)}");
            }
            else
            {
                logger?.Trace($"Empty words");
                words.Clear();
            }
        }
Exemplo n.º 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));
        }