Example #1
0
        private void ProcessAction(SettingsKeyboardKeyAction action, SettingsKeyboardKeyActionType actionType)
        {
            switch (actionType)
            {
            case SettingsKeyboardKeyActionType.LaunchApplication:
                ExecuteLaunchApplication(action);
                break;

            case SettingsKeyboardKeyActionType.Keyboard:
                ExecuteKeyboard(action);
                break;

            case SettingsKeyboardKeyActionType.WindowMessage:
                ExecuteWindowMessage(action);
                break;
            }
        }
Example #2
0
        private void ExecuteLaunchApplication(SettingsKeyboardKeyAction action)
        {
            SettingsKeyboardKeyTypedActionLaunchApplication launchApplication = action.LaunchApplication;

            WriteStatusMessage("Launching application: " + launchApplication.Command);

            string exe  = launchApplication.Command.Trim();
            string args = "";

            if (exe[0] == '"')
            {
                int endOfExeIndex = exe.IndexOf("\" ", 1);
                if (endOfExeIndex != -1)
                {
                    endOfExeIndex++;
                    args = exe.Substring(endOfExeIndex + 1).TrimStart();
                    exe  = exe.Substring(0, endOfExeIndex);
                }
            }
            else
            {
                int endOfExeIndex = exe.IndexOf(" ");
                if (endOfExeIndex != -1)
                {
                    args = exe.Substring(endOfExeIndex + 1).TrimStart();
                    exe  = exe.Substring(0, endOfExeIndex);
                }
            }

            Process process = System.Diagnostics.Process.Start(exe, args);

            if (launchApplication.WaitForInputIdle)
            {
                process.WaitForInputIdle(10000);
            }
            else if (launchApplication.WaitForExit)
            {
                process.WaitForExit(10000);
            }
        }
Example #3
0
        private void ExecuteWindowMessage(SettingsKeyboardKeyAction action)
        {
            SettingsKeyboardKeyTypedActionWindowMessage windowMessage = action.WindowMessage;

            WriteStatusMessage("Sending window message: " + windowMessage.GetDetails());


            var processIds = new HashSet <uint>();

            if (!string.IsNullOrEmpty(windowMessage.ProcessName))
            {
                var processes = Process.GetProcessesByName(windowMessage.ProcessName);
                foreach (var p in processes)
                {
                    processIds.Add((uint)p.Id);
                }
            }

            var windowHandles = new List <IntPtr>();

            Win32.EnumWindowsProc proc = new Win32.EnumWindowsProc((IntPtr hwnd, int lParam) =>
            {
                if (processIds.Count > 0)
                {
                    uint pid;
                    Win32.GetWindowThreadProcessId(hwnd, out pid);
                    if (!processIds.Contains(pid))
                    {
                        return(true);
                    }
                }

                if (!string.IsNullOrEmpty(windowMessage.WindowClass))
                {
                    StringBuilder windowClass = new StringBuilder(Win32.GETWINDOWTEXT_MAXLENGTH);
                    Win32.GetClassName(hwnd, windowClass, windowClass.Capacity);
                    if (!windowMessage.WindowClass.Equals(windowClass.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }

                if (!string.IsNullOrEmpty(windowMessage.WindowName))
                {
                    StringBuilder windowTitle = new StringBuilder(Win32.GETWINDOWTEXT_MAXLENGTH);
                    Win32.GetWindowText(hwnd, windowTitle, windowTitle.Capacity);
                    if (!windowMessage.WindowName.Equals(windowTitle.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }

                windowHandles.Add(hwnd);
                return(true);
            });
            Win32.EnumWindows(proc, 0);

            if (windowHandles.Count == 0)
            {
                if (windowMessage.NotFoundAction != SettingsKeyboardKeyActionType.WindowMessage)
                {
                    ProcessAction(action, windowMessage.NotFoundAction);
                }
                return;
            }

            foreach (var hwnd in windowHandles)
            {
                System.Diagnostics.Debug.WriteLine("SendMessage(" +
                                                   "0x" + ((uint)hwnd.ToInt32()).ToString("x") +
                                                   ", 0x" + windowMessage.Message.ToString("x") +
                                                   ", 0x" + windowMessage.WParam.ToString("x") +
                                                   ", 0x" + windowMessage.LParam.ToString("x") + ")");
                var result = Win32.SendMessage(hwnd, windowMessage.Message, new IntPtr(windowMessage.WParam), new IntPtr(windowMessage.LParam));
                WriteStatusMessage("SendMessage result: 0x" + ((uint)result.ToInt32()).ToString("x"));
            }
        }
Example #4
0
        private void ExecuteKeyboard(SettingsKeyboardKeyAction action)
        {
            SettingsKeyboardKeyTypedActionKeyboard keyboard = action.Keyboard;

            int keysDownCount = KeyboardHookExternal.Current.KeysDownCount();

            if (keysDownCount > 0)
            {
                WriteStatusMessage("   Waiting for all keys to be up before sending new keystroke : keys down = " + keysDownCount.ToString());
                if (KeyboardHookExternal.Current.AllKeysUpEvent.WaitOne(100))
                {
                    WriteStatusMessage("   All keys are up");
                }
                else
                {
                    WriteStatusMessage("   Timeout waiting for all keys to be up");
                }
            }

            WriteStatusMessage("Sending keystroke: " + keyboard.GetDetails());

            for (int repeat = 0; repeat < keyboard.RepeatCount; repeat++)
            {
                //lock (KeyboardSendInputLock)
                {
                    List <Win32.INPUT> inputList = new List <Win32.INPUT>();

                    if (repeat == 0)
                    {
                        List <KeysWithExtended> keysDown = new List <KeysWithExtended>();
                        keysDown.AddRange(KeyboardHookExternal.Current.KeyStateLowLevel.Modifiers);
                        if (KeyboardHookExternal.Current.KeyStateLowLevel.KeyDown)
                        {
                            keysDown.Add(KeyboardHookExternal.Current.KeyStateLowLevel.KeyWithExtended);
                        }
                        bool controlDown = false;
                        bool shiftDown   = false;
                        bool altDown     = false;
                        bool lWinDown    = false;
                        bool rWinDown    = false;
                        foreach (KeysWithExtended key in keysDown)
                        {
                            controlDown |= key.IsControlKey;
                            shiftDown   |= key.IsShiftKey;
                            altDown     |= key.IsAltKey;
                            lWinDown    |= key.IsLWinKey;
                            rWinDown    |= key.IsRWinKey;
                        }

                        if (controlDown)
                        {
                            inputList.Add(CreateInputStruct((ushort)Keys.ControlKey, false));
                        }

                        if (shiftDown)
                        {
                            inputList.Add(CreateInputStruct((ushort)Keys.ShiftKey, false));
                        }

                        if (altDown)
                        {
                            inputList.Add(CreateInputStruct((ushort)Keys.Menu, false));
                        }

                        if (lWinDown)
                        {
                            inputList.Add(CreateInputStruct((ushort)Keys.LWin, false));
                        }

                        if (rWinDown)
                        {
                            inputList.Add(CreateInputStruct((ushort)Keys.RWin, false));
                        }
                    }

                    if (keyboard.Control)
                    {
                        inputList.Add(CreateInputStruct((ushort)Keys.ControlKey, true));
                    }

                    if (keyboard.Shift)
                    {
                        inputList.Add(CreateInputStruct((ushort)Keys.ShiftKey, true));
                    }

                    if (keyboard.Alt)
                    {
                        inputList.Add(CreateInputStruct((ushort)Keys.Menu, true));
                    }

                    if (keyboard.LWin)
                    {
                        inputList.Add(CreateInputStruct((ushort)Keys.LWin, true));
                    }

                    if (keyboard.RWin)
                    {
                        inputList.Add(CreateInputStruct((ushort)Keys.RWin, true));
                    }

                    inputList.Add(CreateInputStruct((ushort)keyboard.VirtualKeyCode, true));

                    inputList.Add(CreateInputStruct((ushort)keyboard.VirtualKeyCode, false));

                    if (keyboard.RWin)
                    {
                        inputList.Add(CreateInputStruct((ushort)Keys.RWin, false));
                    }

                    if (keyboard.LWin)
                    {
                        inputList.Add(CreateInputStruct((ushort)Keys.LWin, false));
                    }

                    if (keyboard.Alt)
                    {
                        inputList.Add(CreateInputStruct((ushort)Keys.Menu, false));
                    }

                    if (keyboard.Shift)
                    {
                        inputList.Add(CreateInputStruct((ushort)Keys.ShiftKey, false));
                    }

                    if (keyboard.Control)
                    {
                        inputList.Add(CreateInputStruct((ushort)Keys.ControlKey, false));
                    }

                    KeyboardRedirectorForm.DisableGlobalKeyboardHook = true;

                    Win32.INPUT[] input  = inputList.ToArray();
                    uint          result = Win32.SendInput(input.Length, input, Marshal.SizeOf(input[0]));

                    KeyboardRedirectorForm.DisableGlobalKeyboardHook = false;
                }
            }
        }
Example #5
0
 private void ProcessAction(SettingsKeyboardKeyAction action)
 {
     ProcessAction(action, action.ActionType);
 }