private static void HandleKeyboardCommand(InputSimulator iis, VirtualKeyCodeContainer macro) { 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[(int)ExtendedCommand.EXTENDED_MACRO_KEY_DOWN]) { RepeatKeyDown(iis, code); //iis.Keyboard.KeyDown(code); } else { dicRepeatKeydown[code] = false; //iis.Keyboard.KeyUp(code); } }
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().Replace("{", "").Replace("}", ""); if (matchText.Length == 1) { char code = matchText.ToUpperInvariant()[0]; keyStrokes.Add(new VirtualKeyCodeContainer((VirtualKeyCode)code)); } 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); }
protected void HandleMacro(string macro, WriterSettings settings, SetKeyTitle setKeyTitleFunction) { 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, settings, setKeyTitleFunction); } else // Normal single keycode { if (!MouseHandler.HandleMouseMacro(iis, keyCode.KeyCode)) { Logger.Instance.LogMessage(TracingLevel.INFO, $"{this.GetType()} KeyPress"); iis.Keyboard.KeyPress(keyCode.KeyCode); } } } } }
public async void SendInput(string inputText, WriterSettings settings, SetKeyTitle setKeyTitleFunction, bool areMacrosSupported = true) { if (String.IsNullOrEmpty(inputText)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"SendInput: Text is null"); return; } if (settings == null) { Logger.Instance.LogMessage(TracingLevel.ERROR, $"SendInput: Settings is null"); 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"); } 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 (!areMacrosSupported || String.IsNullOrWhiteSpace(macro)) // Not a macro, just input the character { InputChar(iis, text[idx], settings); } else if (macro == COMMENT_MACRO) // Comment, ignore everything until the next newline { var newPos = text.IndexOf('\n', idx); // If no newline exists, skip the entire rest of the text if (newPos < 0) { newPos = text.Length; } idx = newPos; } else // This is a macro, run it { idx += macro.Length - 1; macro = macro.Substring(1, macro.Length - 2); HandleMacro(macro, settings, setKeyTitleFunction); } } else { InputChar(iis, text[idx], settings); } Thread.Sleep(settings.Delay); } }); InputRunning = false; }
public async void SendStickyInput(string inputText, WriterSettings settings, SetKeyTitle setKeyTitleFunction, bool areMacrosSupported = true) { if (String.IsNullOrEmpty(inputText)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"SendStickyInput: Text is null"); return; } if (settings == null) { Logger.Instance.LogMessage(TracingLevel.ERROR, $"SendStickyInput: Settings is null"); 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"); } int autoStopNum = settings.AutoStopNum; bool isAutoStopMode = autoStopNum > 0; int counter = autoStopNum; while (StickyEnabled) { for (int idx = 0; idx < text.Length; idx++) { if (!StickyEnabled && !settings.RunUntilEnd) // 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 (!areMacrosSupported || 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, settings, setKeyTitleFunction); } } else { iis.Keyboard.TextEntry(text[idx]); } Thread.Sleep(settings.Delay); } if (isAutoStopMode) { counter--; // First decrease, then check if equals zero if (counter <= 0) { StickyEnabled = false; } } } }); InputRunning = false; }