Пример #1
0
        protected void HandleKeystroke()
        {
            if (String.IsNullOrEmpty(settings.Command))
            {
                return;
            }

            if (settings.Command.Length == 1) // 1 Character is fine
            {
                return;
            }

            string macro = CommandTools.ExtractMacro(settings.Command, 0);

            if (string.IsNullOrEmpty(macro)) // Not a macro, save only first character
            {
                settings.Command = settings.Command[0].ToString();
                SaveSettings();
            }
            else
            {
                if (settings.Command != macro) // Save only one keystroke
                {
                    settings.Command = macro;
                    SaveSettings();
                }
            }
        }
        protected async void SendStickyInput(string inputText)
        {
            if (String.IsNullOrEmpty(inputText))
            {
                return;
            }

            inputRunning = true;
            await Task.Run(() =>
            {
                InputSimulator iis = new InputSimulator();
                string text        = inputText;

                if (settings.IgnoreNewline)
                {
                    text = text.Replace("\r\n", "\n").Replace("\n", "");
                }
                else if (Settings.EnterMode)
                {
                    text = text.Replace("\r\n", "\n");
                }

                while (keyPressed)
                {
                    for (int idx = 0; idx < text.Length; idx++)
                    {
                        if (!keyPressed) // Stop as soon as user presses button
                        {
                            break;
                        }
                        if (Settings.EnterMode && text[idx] == '\n')
                        {
                            iis.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN);
                        }
                        else if (text[idx] == CommandTools.MACRO_START_CHAR)
                        {
                            string macro = CommandTools.ExtractMacro(text, idx);
                            if (String.IsNullOrWhiteSpace(macro)) // Not a macro, just input the character
                            {
                                iis.Keyboard.TextEntry(text[idx]);
                            }
                            else // This is a macro, run it
                            {
                                idx  += macro.Length - 1;
                                macro = macro.Substring(1, macro.Length - 2);

                                HandleMacro(macro);
                            }
                        }
                        else
                        {
                            iis.Keyboard.TextEntry(text[idx]);
                        }
                        Thread.Sleep(Settings.Delay);
                    }
                }
            });

            inputRunning = false;
        }
Пример #3
0
        internal static List <VirtualKeyCodeContainer> ExtractKeyStrokes(string macroText)
        {
            List <VirtualKeyCodeContainer> keyStrokes = new List <VirtualKeyCodeContainer>();


            try
            {
                MatchCollection matches = Regex.Matches(macroText, CommandTools.REGEX_SUB_COMMAND);
                foreach (var match in matches)
                {
                    string matchText = match.ToString().ToUpperInvariant().Replace("{", "").Replace("}", "");
                    if (matchText.Length == 1)
                    {
                        keyStrokes.Add(new VirtualKeyCodeContainer((VirtualKeyCode)matchText[0]));
                    }
                    else
                    {
                        VirtualKeyCodeContainer stroke = CommandTools.MacroTextToKeyCode(matchText);
                        if (stroke != null)
                        {
                            keyStrokes.Add(stroke);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.LogMessage(TracingLevel.ERROR, $"ExtractKeyStrokes Exception: {ex}");
            }

            return(keyStrokes);
        }
Пример #4
0
        protected void RunCommand()
        {
            try
            {
                if (string.IsNullOrEmpty(settings.Command))
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"Command not configured");
                    return;
                }

                if (settings.Command.Length == 1)
                {
                    Task.Run(() => SimulateTextEntry(settings.Command[0]));
                }
                else // KeyStroke
                {
                    List <VirtualKeyCodeContainer> keyStrokes = CommandTools.ExtractKeyStrokes(settings.Command);

                    // Actually initiate the keystrokes
                    if (keyStrokes.Count > 0)
                    {
                        VirtualKeyCodeContainer keyCode = keyStrokes.Last();
                        keyStrokes.Remove(keyCode);

                        if (keyStrokes.Count > 0)
                        {
                            Task.Run(() => SimulateKeyStroke(keyStrokes.Select(ks => ks.KeyCode).ToArray(), keyCode.KeyCode));
                        }
                        else
                        {
                            if (keyCode.IsExtended)
                            {
                                Task.Run(() => SimulateExtendedMacro(keyCode));
                            }
                            else
                            {
                                Task.Run(() => SimulateKeyDown(keyCode.KeyCode));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.LogMessage(TracingLevel.ERROR, $"RunCommand Exception: {ex}");
            }
        }
        protected async void SendInput(string inputText)
        {
            inputRunning = true;
            await Task.Run(() =>
            {
                InputSimulator iis = new InputSimulator();
                string text        = inputText;

                if (settings.EnterMode)
                {
                    text = text.Replace("\r\n", "\n");
                }

                for (int idx = 0; idx < text.Length && !forceStop; idx++)
                {
                    if (settings.EnterMode && text[idx] == '\n')
                    {
                        iis.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN);
                    }
                    else if (text[idx] == CommandTools.MACRO_START_CHAR)
                    {
                        string macro = CommandTools.ExtractMacro(text, idx);
                        if (String.IsNullOrWhiteSpace(macro)) // Not a macro, just input the character
                        {
                            InputChar(iis, text[idx]);
                        }
                        else // This is a macro, run it
                        {
                            idx  += macro.Length - 1;
                            macro = macro.Substring(1, macro.Length - 2);

                            HandleMacro(macro);
                        }
                    }
                    else
                    {
                        InputChar(iis, text[idx]);
                    }
                    Thread.Sleep(settings.Delay);
                }
            });

            inputRunning = false;
        }
Пример #6
0
        protected void HandleMacro(string macro)
        {
            List <VirtualKeyCodeContainer> keyStrokes = CommandTools.ExtractKeyStrokes(macro);

            // Actually initiate the keystrokes
            if (keyStrokes.Count > 0)
            {
                InputSimulator          iis     = new InputSimulator();
                VirtualKeyCodeContainer keyCode = keyStrokes.Last();
                keyStrokes.Remove(keyCode);

                if (keyStrokes.Count > 0)
                {
                    if (settings.KeydownDelay)
                    {
                        Logger.Instance.LogMessage(TracingLevel.INFO, $"{this.GetType()} DelayedModifiedKeyStroke");
                        iis.Keyboard.DelayedModifiedKeyStroke(keyStrokes.Select(ks => ks.KeyCode).ToArray(), new VirtualKeyCode[] { keyCode.KeyCode }, settings.Delay);
                    }
                    else
                    {
                        Logger.Instance.LogMessage(TracingLevel.INFO, $"{this.GetType()} ModifiedKeyStroke");
                        iis.Keyboard.ModifiedKeyStroke(keyStrokes.Select(ks => ks.KeyCode).ToArray(), keyCode.KeyCode);
                    }
                }
                else // Single Keycode
                {
                    if (keyCode.IsExtended)
                    {
                        Logger.Instance.LogMessage(TracingLevel.INFO, $"{this.GetType()} HandleExtendedMacro");
                        ExtendedMacroHandler.HandleExtendedMacro(iis, keyCode);
                    }
                    else // Normal single keycode
                    {
                        if (!MouseHandler.HandleMouseMacro(iis, keyCode.KeyCode))
                        {
                            Logger.Instance.LogMessage(TracingLevel.INFO, $"{this.GetType()} KeyPress");
                            iis.Keyboard.KeyPress(keyCode.KeyCode);
                        }
                    }
                }
            }
        }
        public static void HandleExtendedMacro(InputSimulator iis, VirtualKeyCodeContainer macro)
        {
            try
            {
                // Check if it's a pause command
                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_PAUSE])
                {
                    if (Int32.TryParse(macro.ExtendedData, out int pauseLength))
                    {
                        Thread.Sleep(pauseLength);
                        return;
                    }
                }

                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_KEY_DOWN] || macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_KEY_UP])
                {
                    string commandText = CommandTools.ConvertSimilarMacroCommands(macro.ExtendedData.ToUpperInvariant());
                    if (string.IsNullOrEmpty(commandText))
                    {
                        Logger.Instance.LogMessage(TracingLevel.ERROR, $"Extended Keydown/Keyup - Missing Command");
                        return;
                    }

                    if (!Enum.TryParse <VirtualKeyCode>(commandText, true, out VirtualKeyCode code))
                    {
                        if (commandText.Length > 1)
                        {
                            Logger.Instance.LogMessage(TracingLevel.WARN, $"Extended Keydown/Keyup Shrinking {commandText} to {commandText[0]}");
                        }
                        code = (VirtualKeyCode)commandText[0];
                    }

                    if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_KEY_DOWN])
                    {
                        RepeatKeyDown(iis, code);
                        //iis.Keyboard.KeyDown(code);
                    }
                    else
                    {
                        dicRepeatKeydown[code] = false;
                        //iis.Keyboard.KeyUp(code);
                    }

                    return;
                }

                // Variables
                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_VARIABLE_INPUT])
                {
                    using (InputBox input = new InputBox("Variable Input", $"Enter value for \"{macro.ExtendedData}\":"))
                    {
                        input.ShowDialog();

                        // Value exists (cancel button was NOT pressed)
                        if (!string.IsNullOrEmpty(input.Input))
                        {
                            dicVariables[macro.ExtendedData] = input.Input;
                        }
                    }
                    return;
                }

                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_VARIABLE_OUTPUT])
                {
                    if (dicVariables.ContainsKey(macro.ExtendedData))
                    {
                        iis.Keyboard.TextEntry(dicVariables[macro.ExtendedData]);
                    }
                    else
                    {
                        Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Output called for {macro.ExtendedData} without an Input beforehand");
                    }
                    return;
                }

                // Mouse Move commands
                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_MOUSE_MOVE] || macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_MOUSE_POS])
                {
                    string[] mousePos = macro.ExtendedData.Split(',');
                    if (mousePos.Length == 2)
                    {
                        if (Double.TryParse(mousePos[0], out double x))
                        {
                            if (Double.TryParse(mousePos[1], out double y))
                            {
                                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_MOUSE_POS])
                                {
                                    iis.Mouse.MoveMouseToPositionOnVirtualDesktop(x, y);
                                }
                                else
                                {
                                    iis.Mouse.MoveMouseBy((int)x, (int)y);
                                }
                            }
                        }
                    }
                    return;
                }

                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_SCROLL_UP] || macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_SCROLL_DOWN])
                {
                    int direction = (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_SCROLL_UP]) ? 1 : -1;
                    iis.Mouse.VerticalScroll(direction);
                    return;
                }

                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_SCROLL_LEFT] || macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_SCROLL_RIGHT])
                {
                    int direction = (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_SCROLL_RIGHT]) ? 1 : -1;
                    iis.Mouse.HorizontalScroll(direction);
                    return;
                }

                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_MOUSE_LEFT_DOWN])
                {
                    iis.Mouse.LeftButtonDown();
                    return;
                }

                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_MOUSE_LEFT_UP])
                {
                    iis.Mouse.LeftButtonUp();
                    return;
                }

                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_MOUSE_RIGHT_DOWN])
                {
                    iis.Mouse.RightButtonDown();
                    return;
                }

                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_MOUSE_RIGHT_UP])
                {
                    iis.Mouse.RightButtonUp();
                    return;
                }

                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_MOUSE_MIDDLE_DOWN])
                {
                    iis.Mouse.MiddleButtonDown();
                    return;
                }

                if (macro.ExtendedCommand == EXTENDED_COMMANDS_LIST[EXTENDED_MACRO_MOUSE_MIDDLE_UP])
                {
                    iis.Mouse.MiddleButtonUp();
                    return;
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.LogMessage(TracingLevel.ERROR, $"Failed to parse extended macro: {macro?.ExtendedCommand} {macro?.ExtendedData} {ex}");
            }
        }