예제 #1
0
        private void OnKeyPressed(GlobalKeyPressed obj)
        {
            if (currentShortcutWindow != null)
            {
                switch (obj.Key)
                {
                case 0x0000001B:     //Escape
                    obj.ProcessKey = false;
                    CloseShortcutWindow();
                    break;

                case 0x00000028:     //Down
                    shortcutViewModel?.IncrementSelection(1);
                    break;

                case 0x00000026:     //Up
                    shortcutViewModel?.IncrementSelection(-1);
                    break;

                case 0x0000000D:     //Enter
                    obj.ProcessKey = false;

                    //If the current shortcut window is open and doesn't have args, then execute it
                    if (currentShortcutWindow != null)
                    {
                        isPasting = true;

                        var selectedItem = shortcutViewModel.SelectedItem;
                        CloseShortcutWindow();
                        Clipboard.SetDataObject(selectedItem);

                        Thread.Sleep(5);
                        System.Windows.Forms.SendKeys.SendWait("^{v}");
                        Console.WriteLine("PASTED FROM SHORTCUT WINDOW!!!!");

                        isPasting = false;
                    }
                    break;
                }
            }
            else if (Keyboard.Modifiers == (ModifierKeys.Shift | ModifierKeys.Control) && obj.Key == (int)System.Windows.Forms.Keys.V)
            {
                currentShortcutWindow             = new ShortcutWindow();
                currentShortcutWindow.DataContext = shortcutViewModel;
                currentShortcutWindow.Show();
                currentShortcutWindow.Activate();
                obj.ProcessKey = false; //Don't process key in order to avoid pasting
            }

            //if (Keyboard.Modifiers == (ModifierKeys.Control) && obj.Key == (int)System.Windows.Forms.Keys.V && isPasting)
            //{
            //    //currentShortcutWindow = new ShortcutWindow();
            //    //currentShortcutWindow.DataContext = shortcutViewModel;
            //    //currentShortcutWindow.Show();
            //    //currentShortcutWindow.Activate();
            //    Console.WriteLine("PASTED FROM KEYBOARD!!");
            //    obj.ProcessKey = false; //Don't process key in order to avoid pasting
            //}
        }
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                var globalKeyPressed = new GlobalKeyPressed()
                {
                    Key = Marshal.ReadInt32(lParam)
                };
                eventBus.Publish(globalKeyPressed);
                if (!globalKeyPressed.ProcessKey)
                {
                    return(new IntPtr(1));
                }
            }

            return(CallNextHookEx(hookId, nCode, wParam, lParam));
        }
 private void Initialize()
 {
     this.callback = (int nCode, IntPtr wParam, IntPtr lParam) =>
     {
         HookStruct info = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct));
         bool       flag = nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN;
         if (flag)
         {
             int vkCode = Marshal.ReadInt32(lParam);
             GlobalKeyPressed?.Invoke(KeyInterop.KeyFromVirtualKey(vkCode));
         }
         if (LockAllKeys && info.flags != LLKHF_INJECTED)
         {
             return((IntPtr)1);
         }
         else
         {
             return(CallNextHookEx(_hookID, nCode, wParam, lParam));
         }
     };
     _hookID = SetHook(this.callback);
 }
예제 #4
0
        /// <summary>
        /// A callback function which will be called every Time a keyboard activity detected.
        /// </summary>
        /// <param name="nCode">
        /// [in] Specifies whether the hook procedure must process the message.
        /// If nCode is HC_ACTION, the hook procedure must process the message.
        /// If nCode is less than zero, the hook procedure must pass the message to the
        /// CallNextHookEx function without further processing and must return the
        /// value returned by CallNextHookEx.l
        /// </param>
        /// <param name="wParam">
        /// [in] Specifies whether the message was sent by the current thread.
        /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero.
        /// </param>
        /// <param name="lParam">
        /// [in] Pointer to a CWPSTRUCT structure that contains details about the message.
        /// </param>
        /// <returns>
        /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
        /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx
        /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC
        /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook
        /// procedure does not call CallNextHookEx, the return value should be zero.
        /// </returns>
        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            //indicates if any of underlaing events set e.Handled flag
            var handled = false;

            if (nCode >= 0)
            {
                //read structure KeyboardHookStruct at lParam
                var keyStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                //raise KeyDown
                if (GlobalKeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
                {
                    var keyEventArgs = new GlobalKeyEventHandlerArgs(
                        keyStruct.VirtualKeyCode,
                        keyStruct.ScanCode,
                        keyStruct.Flags,
                        keyStruct.Time,
                        keyStruct.ExtraInfo,
                        null);

                    GlobalKeyDown.Invoke(null, keyEventArgs);
                    handled = keyEventArgs.Handled;
                }

                // raise KeyPress
                if (GlobalKeyPressed != null && wParam == WM_KEYDOWN)
                {
                    var isDownShift    = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);
                    var isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);

                    var keyState = new byte[256];
                    GetKeyboardState(keyState);
                    var inBuffer = new byte[2];

                    if (ToAscii(keyStruct.VirtualKeyCode,
                                keyStruct.ScanCode,
                                keyState,
                                inBuffer,
                                keyStruct.Flags) == 1)
                    {
                        var key = (char)inBuffer[0];
                        if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key))
                        {
                            key = Char.ToUpper(key);
                        }

                        var keyEventArgs = new GlobalKeyEventHandlerArgs(
                            keyStruct.VirtualKeyCode,
                            keyStruct.ScanCode,
                            keyStruct.Flags,
                            keyStruct.Time,
                            keyStruct.ExtraInfo,
                            key);

                        GlobalKeyPressed.Invoke(null, keyEventArgs);
                        handled = handled || keyEventArgs.Handled;
                    }
                }

                // raise KeyUp
                if (GlobalKeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
                {
                    var keyEventArgs = new GlobalKeyEventHandlerArgs(
                        keyStruct.VirtualKeyCode,
                        keyStruct.ScanCode,
                        keyStruct.Flags,
                        keyStruct.Time,
                        keyStruct.ExtraInfo,
                        null);

                    GlobalKeyUp.Invoke(null, keyEventArgs);
                    handled = handled || keyEventArgs.Handled;
                }
            }

            //if event handled in application do not handoff to other listeners
            if (handled)
            {
                return(-1);
            }

            //forward to other application
            return(CallNextHookEx(_windowsKeyboardHookHandle, nCode, wParam, lParam));
        }
        private void OnKeyPressed(GlobalKeyPressed obj)
        {
            if (currentShortcutWindow != null || currentArgumentsWindow != null)
            {
                switch (obj.Key)
                {
                case 0x0000001B:     //Escape
                    obj.ProcessKey = false;
                    CloseShortcutWindow();
                    CloseArgumentWindow();
                    break;

                case 0x00000028:     //Down
                    shortcutViewModel?.IncrementSelection(1);
                    break;

                case 0x00000026:     //Up
                    shortcutViewModel?.IncrementSelection(-1);
                    break;

                case 0x0000000D:     //Enter
                    obj.ProcessKey = false;

                    //If the current shortcut window is open and doesn't have args, then execute it
                    if (currentShortcutWindow != null && !IsArgumentShortcut())
                    {
                        shortcutViewModel.ExecuteSelection();
                        CloseShortcutWindow();
                        break;
                    }

                    //If the arg window is open and it's not filled out then open the arg window
                    if (currentArgumentsWindow != null && IsArgumentShortcut() && !IsArgumentShortcutFilled())
                    {
                        argumentsViewModel.ExecuteArgument();

                        var command   = argumentsViewModel.Command;
                        var arguments = GetArgs();

                        CloseArgumentWindow();

                        if (arguments.Count() <= shortcutExecutor.Arguments.Count)
                        {
                            shortcutExecutor.Execute(command.Title, command);
                            break;
                        }

                        var argumentKey = arguments.ElementAt(shortcutExecutor.Arguments.Count);

                        argumentsViewModel.Command     = command;
                        argumentsViewModel.ArgumentKey = argumentKey;

                        if (IsArgumentShortcutFilled())
                        {
                            break;
                        }

                        OpenArgumentWindow();
                    }

                    //If the shortcut window is open and it has args, then open the arg window
                    if (currentShortcutWindow != null && IsArgumentShortcut() && !IsArgumentShortcutFilled())
                    {
                        var command     = shortcutViewModel.SelectedItem.Value;
                        var argumentKey = GetArgs().ElementAt(shortcutExecutor.Arguments.Count);

                        CloseShortcutWindow();

                        argumentsViewModel.Command     = command;
                        argumentsViewModel.ArgumentKey = argumentKey;

                        OpenArgumentWindow();
                        break;
                    }

                    //if the arg window is open and it's filled out, then execute the shortcut
                    if (currentArgumentsWindow != null && IsArgumentShortcut() && IsArgumentShortcutFilled())
                    {
                        argumentsViewModel.Execute();
                        CloseArgumentWindow();
                        break;
                    }

                    break;
                }
            }
            else if (Keyboard.Modifiers == (settingsViewModel.Modifier1 | settingsViewModel.Modifier2) && obj.Key == (int)settingsViewModel.Key) //Space
            {
                currentShortcutWindow             = new ShortcutWindow();
                currentShortcutWindow.DataContext = shortcutViewModel;
                currentShortcutWindow.Show();
                currentShortcutWindow.Activate();

                obj.ProcessKey = false;

                shortcutViewModel.Reset();
            }
        }