private void btnAutomationChooseWindow_Click(object sender, RoutedEventArgs e)
        {
            if (_automationChosenProc == null)
            {
                Utilities.Message("Please choose process first.");
                return;
            }

            //Structs.WindowInfo windowInfo;
            var allWindowHandles = Managed.GetAllWindows(_automationChosenProc.Process)
                .OfType<IntPtr>()
                .Distinct();
            //.Where(item => ((windowInfo = Managed.GetWindowInfo(item)).dwStyle.HasFlag(Enums.WindowStyles.WS_CAPTION) &&
            //    windowInfo.dwStyle.HasFlag(Enums.WindowStyles.WS_VISIBLE)) ||
            //    windowInfo.dwExStyle.HasFlag(Enums.WindowStylesEx.WS_EX_APPWINDOW)).ToList();

            string title = null;
            string className = null;
            Structs.WindowInfo windowInfo;
            var allWindowHandlesDictionary = allWindowHandles
                .ToDictionary(
                    item => (int)item,
                    item => (
                        (!string.IsNullOrWhiteSpace((title = Managed.GetWindowText(item))) ? title : string.Empty) + ";" +
                        (!string.IsNullOrWhiteSpace(className = Managed.GetClassName(item)) ? "ClassName: " + className : string.Empty) + ";Visible: " +
                        ((windowInfo = Managed.GetWindowInfo(item)).dwStyle.HasFlag(Enums.WindowStyles.WS_CAPTION) ? "True" : "False")
                    )
                );

            var chooseWindow = new ItemChoose((windowHandle) =>
            {
                var ptrWindowHandle = (IntPtr)windowHandle;
                _automationChosenWindow = new ProcessWindow(_automationChosenProc.Id, ptrWindowHandle, Managed.GetWindowText(ptrWindowHandle));
                var content = string.Concat("WindowHandle: " + windowHandle, " Name: " + _automationChosenWindow.Title);
                lblAutomationChosenWindow.Content = content;
            },
            allWindowHandlesDictionary,
            isOrderByKey: false,
            onCancel: () =>
            {
                _automationChosenWindow = null;
                lblAutomationChosenWindow.Content = "No Window Chosen";
            });
            chooseWindow.ShowDialog();
        }
 private void ListBoxAutomationWindows_Item_Selected(object sender, RoutedEventArgs e)
 {
     _automationSelectedWindow = (sender as ListBoxItem).Content as ProcessWindow;
     if (_automationSelectedWindow != null)
     {
         lblAutomationSelectedWindow.Content = _automationSelectedWindow.Title;
         AutomationUpdateInputEvents();
     }
 }
        public static void SimulateInput(AutomationType automationType, ProcessWindow processWindow, VirtualKeyCode vkKey, InputType inputType, KeyState keyState = KeyState.Press)
        {
            if (automationType == AutomationType.Foreground)
            {
                ActivateWindow(processWindow.ProcessID, processWindow.Handle);
            }

            if (keyState == KeyState.Press)
            {
                SimulateInput(automationType, processWindow, vkKey, inputType, keyState: KeyState.Down);
                SimulateInput(automationType, processWindow, vkKey, inputType, keyState: KeyState.Up);
            }
            else
            {
                uint windowMessage = 0;
                IntPtr lParam = IntPtr.Zero;
                switch (inputType)
                {
                    case InputType.Keyboard:
                        {
                            if (keyState == KeyState.Down)
                            {
                                windowMessage = (uint)Enums.WMessages.KEYDOWN;
                                lParam = GetLParam(1, vkKey, 0, 0, 0, 0);
                            }
                            else if (keyState == KeyState.Up)
                            {
                                windowMessage = (uint)Enums.WMessages.KEYUP;
                                lParam = GetLParam(1, vkKey, 0, 0, 1, 1);
                            }
                            break;
                        }
                    case InputType.Mouse:
                        {
                            windowMessage = (uint)(keyState == KeyState.Down ? Enums.WMessages.KEYDOWN : Enums.WMessages.KEYUP);
                            if (keyState == KeyState.Down)
                            {
                                windowMessage = (uint)Enums.WMessages.KEYDOWN;
                                lParam = GetLParam(1, vkKey, 0, 0, 0, 0);
                            }
                            else if (keyState == KeyState.Up)
                            {
                                windowMessage = (uint)Enums.WMessages.KEYUP;
                                lParam = GetLParam(1, vkKey, 0, 0, 1, 1);
                            }
                            break;
                        }
                }

                if (automationType == Automation.AutomationType.Background)
                {
                    var result = NativeMethods.SendMessage(processWindow.Handle, windowMessage, (IntPtr)vkKey, lParam);
                    if (result != IntPtr.Zero)
                    {
                        if (keyState == KeyState.Down)
                            NativeMethods.SendMessage(processWindow.Handle, (uint)Enums.WMessages.CHAR, (IntPtr)vkKey, lParam);
                    }
                }
                else
                {
                    switch (keyState)
                    {
                        case KeyState.Down:
                            {
                                InputSimulator.Keyboard.KeyDown(vkKey);
                                break;
                            }
                        case KeyState.Up:
                            {
                                InputSimulator.Keyboard.KeyUp(vkKey);
                                break;
                            }
                    }
                }
            }
        }
        private void btnAutomationChooseProcess_Click(object sender, RoutedEventArgs e)
        {
            _automationChosenWindow = null;
            lblAutomationChosenWindow.Content = "No Window Chosen";

            var chooseProcess = new ItemChoose((processID) =>
            {
                _automationChosenProc = new Proc(Process.GetProcessById(processID));
                var content = string.Concat("PID: " + _automationChosenProc.Id, " Name: " + _automationChosenProc.Name);
                lblAutomationChosenProcess.Content = content;
            },
            Proc.GetAllProcesses(false).ToDictionary(item => item.Id, item => item.Name),
            isOrderByKey: false,
            onCancel: () =>
            {
                _automationChosenProc = null;
                lblAutomationChosenProcess.Content = "No Process Chosen";
            });
            chooseProcess.ShowDialog();
        }
 public static void SimulateInput(AutomationType automationType, ProcessWindow processWindow, char keyChar)
 {
     var vkKey = ConvertCharToVirtualKey(keyChar);
     SimulateInput(automationType, processWindow, vkKey, InputType.Keyboard);
 }
 public static void SimulateInput(AutomationType automationType, ProcessWindow processWindow, string text)
 {
     if (automationType == Automation.AutomationType.Foreground)
     {
         NativeMethods.AllowSetForegroundWindow(processWindow.ProcessID);
         NativeMethods.SetForegroundWindow(processWindow.Handle);
         Thread.Sleep(10);
         InputSimulator.Keyboard.TextEntry(text);
     }
     else
     {
         foreach (var keyChar in text)
         {
             SimulateInput(automationType, processWindow, keyChar);
         }
     }
 }
 public WindowInput(ProcessWindow window, AutomationType automationType, List<InputEvent> inputEvents)
 {
     Window = window;
     AutomationType = automationType;
     InputEvents = inputEvents;
 }
 public static void SimulateForegroundMousePosition(ProcessWindow processWindow, double x, double y)
 {
     ActivateWindow(processWindow.ProcessID, processWindow.Handle);
     InputSimulator.Mouse.MoveMouseTo(x, y);
     Thread.Sleep(10);
 }
 public static void SimulateForegroundMouseClick(ProcessWindow processWindow, VirtualKeyCode vkKey, double x, double y, KeyState keyState = KeyState.Press)
 {
     SimulateForegroundMousePosition(processWindow, x, y);
     switch (keyState)
     {
         case KeyState.Press:
             {
                 switch (vkKey)
                 {
                     case VirtualKeyCode.MBUTTON:
                         {
                             break;
                         }
                     case VirtualKeyCode.LBUTTON:
                         {
                             InputSimulator.Mouse.LeftButtonClick();
                             break;
                         }
                     case VirtualKeyCode.RBUTTON:
                         {
                             InputSimulator.Mouse.RightButtonClick();
                             break;
                         }
                 }
                 break;
             }
         case KeyState.Down:
             {
                 switch (vkKey)
                 {
                     case VirtualKeyCode.MBUTTON:
                         {
                             break;
                         }
                     case VirtualKeyCode.LBUTTON:
                         {
                             InputSimulator.Mouse.LeftButtonDown();
                             break;
                         }
                     case VirtualKeyCode.RBUTTON:
                         {
                             InputSimulator.Mouse.RightButtonDown();
                             break;
                         }
                 }
                 break;
             }
         case KeyState.Up:
             {
                 switch (vkKey)
                 {
                     case VirtualKeyCode.MBUTTON:
                         {
                             break;
                         }
                     case VirtualKeyCode.LBUTTON:
                         {
                             InputSimulator.Mouse.LeftButtonUp();
                             break;
                         }
                     case VirtualKeyCode.RBUTTON:
                         {
                             InputSimulator.Mouse.RightButtonUp();
                             break;
                         }
                 }
                 break;
             }
     }
 }