private void btnAutomationMouse_Click(object sender, RoutedEventArgs e)
        {
            if (_automationSelectedWindow == null)
            {
                Utilities.Message("Please first select window for input automation.");
                return;
            }

            var mouseInput = new MouseInput((data, representation) =>
            {
                object inputData = data;
                Automation.InputDataType dataType = InputDataType.VKXY;

                _automationCurrentInputEvent = new InputEvent(inputData, null, representation, dataType, Automation.InputType.Mouse);
                txtAutomation.Text = representation;
            }, () =>
            {
                _automationCurrentInputEvent = null;
                txtAutomation.Text = "";
            });
            mouseInput.ShowDialog();
        }
        private void btnAutomationKeyboard_Click(object sender, RoutedEventArgs e)
        {
            if (_automationSelectedWindow == null)
            {
                Utilities.Message("Please first select window for input automation.");
                return;
            }

            var keyboardInput = new KeyboardInput((key, modifiers, text, stringRepresentation) =>
            {
                object inputData = null;
                Automation.InputDataType dataType;
                if (text != null)
                {
                    inputData = text;
                    dataType = Automation.InputDataType.String;
                }
                else
                {
                    inputData = key.Value;
                    dataType = Automation.InputDataType.VirtualKey;
                }

                _automationCurrentInputEvent = new InputEvent(inputData, modifiers, stringRepresentation, dataType, Automation.InputType.Keyboard);
                txtAutomation.Text = stringRepresentation;
            }, () =>
            {
                _automationCurrentInputEvent = null;
                txtAutomation.Text = "";
            });
            keyboardInput.ShowDialog();
        }
 private void AutomationUpdateInputEvents(InputEvent inputEvent = null, bool isAdd = true)
 {
     if (inputEvent == null)
     {
         _automationWindowInputsViewItems.Clear();
         WindowInput windowInput = null;
         if (_automationWindowInputsDictionary.TryGetValue(_automationSelectedWindow.Handle, out windowInput))
         {
             if (windowInput != null)
             {
                 _automationWindowInputsViewItems.AddRange(windowInput.InputEvents);
             }
         }
     }
     else
     {
         object selectedItem = inputEvent;
         if (isAdd)
             _automationWindowInputsViewItems.Add(inputEvent);
         else
         {
             _automationWindowInputsViewItems.Remove(inputEvent);
             if (AutomationWindowInputsDataGrid.Items.Count > 0)
                 selectedItem = AutomationWindowInputsDataGrid.Items[0];
             else
                 selectedItem = null;
         }
         AutomationWindowInputsDataGrid.SelectedItem = selectedItem;
     }
 }
        private void btnAutomationAdd_Click(object sender, RoutedEventArgs e)
        {
            if (_automationSelectedWindow != null && _automationCurrentInputEvent != null)
            {
                int cycle = 0;
                int duration = 0;
                int.TryParse(txtCycle.Text, out cycle);
                int.TryParse(txtDuration.Text, out duration);
                _automationCurrentInputEvent.CycleCount = cycle;
                _automationCurrentInputEvent.Duration = duration;

                var automationWindowInputs = _automationWindowInputsDictionary[_automationSelectedWindow.Handle];
                bool isCreate = automationWindowInputs == null;
                if (isCreate)
                    automationWindowInputs = new WindowInput(_automationSelectedWindow, (AutomationType)Enum.Parse(typeof(AutomationType), cbAutomationType.Text), new List<InputEvent>());

                automationWindowInputs.InputEvents.Add(_automationCurrentInputEvent);
                if (isCreate)
                    _automationWindowInputsDictionary[_automationSelectedWindow.Handle] = automationWindowInputs;
                AutomationUpdateInputEvents(_automationCurrentInputEvent);
                _automationCurrentInputEvent = null;
                txtAutomation.Text = "";
            }
        }
        public void SimulateInput(InputEvent inputEvent)
        {
            if (inputEvent.Modifiers != null)
            {
                foreach (var modifier in inputEvent.Modifiers)
                {
                    SimulateInput(AutomationType, Window, modifier, inputEvent.InputType, keyState: KeyState.Down);
                }
            }

            switch (inputEvent.InputDataType)
            {
                case InputDataType.VirtualKey:
                    {
                        VirtualKeyCode vkKey = (VirtualKeyCode)inputEvent.InputData;
                        SimulateInput(AutomationType, Window, vkKey, inputEvent.InputType);
                        break;
                    }
                case InputDataType.Char:
                    {
                        char keyChar = (char)inputEvent.InputData;
                        SimulateInput(AutomationType, Window, keyChar);
                        break;
                    }
                case InputDataType.String:
                    {
                        string text = (string)inputEvent.InputData;
                        SimulateInput(AutomationType, Window, text);
                        break;
                    }
                case InputDataType.VKXY:
                    {
                        double[] data = (double[])inputEvent.InputData;
                        var vkKeyUint = (uint)data[0];
                        bool isMoveOnly = vkKeyUint == 0;

                        var xPosition = (int)data[1];
                        var yPoistion = (int)data[2];
                        var xAbsolute = (double)data[3];
                        var yAbsolute = (double)data[4];
                        switch (AutomationType)
                        {
                            case AutomationType.Foreground:
                                {
                                    if (isMoveOnly)
                                        SimulateForegroundMousePosition(Window, xAbsolute, yAbsolute);
                                    else
                                        SimulateForegroundMouseClick(Window, (VirtualKeyCode)vkKeyUint, xAbsolute, yAbsolute);
                                    break;
                                }
                            case AutomationType.Background:
                                {
                                    if (isMoveOnly)
                                        SimulateBackgroundMousePosition(Window.Handle, xPosition, yPoistion);
                                    else
                                        SimulateBackgroundMouseClick(Window.Handle, (VirtualKeyCode)vkKeyUint, xPosition, yPoistion);
                                    break;
                                }
                        }
                        break;
                    }
            }

            if (inputEvent.Modifiers != null)
            {
                foreach (var modifier in inputEvent.Modifiers)
                {
                    SimulateInput(AutomationType, Window, modifier, inputEvent.InputType, keyState: KeyState.Up);
                }
            }

            if (inputEvent.Duration > 0)
            {
                Thread.Sleep(inputEvent.Duration);
            }
        }