Exemple #1
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);
        }
Exemple #2
0
 private void SimulateExtendedMacro(VirtualKeyCodeContainer keyCode)
 {
     Logger.Instance.LogMessage(TracingLevel.INFO, $"{this.GetType()} SimulateExtendedMacro");
     while (keyPressed)
     {
         ExtendedMacroHandler.HandleExtendedMacro(iis, keyCode);
         Thread.Sleep(30);
     }
 }
Exemple #3
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}");
            }
        }
Exemple #4
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}");
            }
        }