protected void InputChar(InputSimulator iis, char c, WriterSettings settings) { if (settings.ForcedMacro) { VirtualKeyCode vk = VirtualKeyCode.LBUTTON; iis.Keyboard.KeyPress(vk.FromChar(c)); } else { iis.Keyboard.TextEntry(c); } }
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; }
private static async Task HandleVariableCommand(ExtendedCommand command, VirtualKeyCodeContainer macro, WriterSettings settings) { string upperExtendedData = macro.ExtendedData.ToUpperInvariant(); // Variables if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_INPUT) { string defaultValue = String.Empty; if (!String.IsNullOrEmpty(upperExtendedData) && dicVariables.ContainsKey(upperExtendedData)) { defaultValue = dicVariables[upperExtendedData]; } using InputBox input = new InputBox("Variable Input", $"Enter value for \"{upperExtendedData}\":", defaultValue); input.ShowDialog(); // Value exists (cancel button was NOT pressed) if (!string.IsNullOrEmpty(input.Input)) { dicVariables[upperExtendedData] = input.Input; } } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_OUTPUT) { if (dicVariables.ContainsKey(upperExtendedData)) { SuperMacroWriter textWriter = new SuperMacroWriter(); await textWriter.SendInput(dicVariables[upperExtendedData], settings, null, false); } else { Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Output called for {upperExtendedData} without an Input beforehand"); } } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_UNSETALL) { dicVariables.Clear(); } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_UNSET) { if (string.IsNullOrEmpty(upperExtendedData)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Unset called without variable name"); return; } if (dicVariables.ContainsKey(upperExtendedData)) { dicVariables.Remove(upperExtendedData); } } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET || command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE) { if (string.IsNullOrEmpty(macro.ExtendedData)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Set called without variable name"); return; } // last parameter "2" ensures maximum of two splits! var splitData = macro.ExtendedData.Split(new char[] { ':' }, 2); if (splitData.Length != 2) { Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Set called without incorrect extended data: {macro.ExtendedData}"); return; } string varInput = splitData[1]; string fileName = TryExtractVariable(splitData[1]); // Set From File but file doesn't exist if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE && !File.Exists(fileName)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable SetFromFile called but file does not exist {fileName}"); return; } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE) // File DOES exist { varInput = File.ReadAllText(fileName); } dicVariables[splitData[0].ToUpperInvariant()] = varInput; } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_CLIPBOARD) { var value = ReadFromClipboard(); // Value exists (cancel button was NOT pressed) if (!string.IsNullOrEmpty(value)) { dicVariables[upperExtendedData] = value; } } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_OUTPUT_TO_FILE) { if (string.IsNullOrEmpty(macro.ExtendedData)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile called without any params"); return; } var splitData = macro.ExtendedData.Split(new char[] { ':' }, 2); if (splitData.Length != 2) { Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile called without incorrect extended data: {macro.ExtendedData}"); return; } string variableName = splitData[0].ToUpperInvariant(); string fileName = TryExtractVariable(splitData[1]); // Check if variable exists if (!dicVariables.ContainsKey(variableName)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile called without non existing variable: {variableName}"); return; } // Try to save the data in the variable to the filename try { File.WriteAllText(fileName, dicVariables[variableName]); } catch (Exception ex) { Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile exception: {macro.ExtendedData} {ex}"); } return; } else { Logger.Instance.LogMessage(TracingLevel.ERROR, $"HandleVariableCommand - Invalid command {command}"); } }
public static async Task HandleExtendedMacro(InputSimulator iis, VirtualKeyCodeContainer macro, WriterSettings settings, SetKeyTitle SetKeyTitleFunction) { try { // Index in array int index = EXTENDED_COMMANDS_LIST.ToList().FindIndex(cmd => cmd == macro.ExtendedCommand); ExtendedCommand command = (ExtendedCommand)index; // Check if this is a function command if (command == ExtendedCommand.EXTENDED_MACRO_FUNCTIONS) { FunctionsHandler.HandleFunctionRequest(macro.ExtendedData, dicVariables); } // Check if it's a pause command else if (command == ExtendedCommand.EXTENDED_MACRO_PAUSE) { string pauseLengthParam = TryExtractVariable(macro.ExtendedData); if (Int32.TryParse(pauseLengthParam, out int pauseLength)) { Thread.Sleep(pauseLength); return; } } // Keyboard commands else if (command == ExtendedCommand.EXTENDED_MACRO_KEY_DOWN || command == ExtendedCommand.EXTENDED_MACRO_KEY_UP) { HandleKeyboardCommand(iis, macro); return; } // Mouse commands else if (index >= (int)ExtendedCommand.EXTENDED_MACRO_MOUSE_MOVE && index <= (int)ExtendedCommand.EXTENDED_MACRO_MOUSE_RESTORE_LOCATION) { HandleMouseCommand(command, iis, macro); } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_INPUT || command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_OUTPUT || command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_UNSETALL || command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_UNSET || command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET || command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE || command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_CLIPBOARD || command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_OUTPUT_TO_FILE) { await HandleVariableCommand(command, macro, settings); } else if (command == ExtendedCommand.EXTENDED_MACRO_STREAMDECK_SETKEYTITLE) { if (SetKeyTitleFunction == null) { Logger.Instance.LogMessage(TracingLevel.ERROR, $"SETKEYTITLE called but callback function is null"); return; } string titleString = TryExtractVariable(macro.ExtendedData).Replace(@"\n", "\n"); SetKeyTitleFunction(titleString); } else if (command == ExtendedCommand.EXTENDED_MACRO_STREAMDECK_SETCLIPBOARD) { SetClipboard(TryExtractVariable(macro.ExtendedData)); } else { Logger.Instance.LogMessage(TracingLevel.ERROR, $"HandleExtendedMacro - Invalid command {command}"); } } catch (Exception ex) { Logger.Instance.LogMessage(TracingLevel.ERROR, $"Failed to parse extended macro: {macro?.ExtendedCommand} {macro?.ExtendedData} {ex}"); } }
private static void HandleVariableCommand(ExtendedCommand command, InputSimulator iis, VirtualKeyCodeContainer macro, WriterSettings settings, SDConnection connection) { string upperExtendedData = macro.ExtendedData.ToUpperInvariant(); // Variables if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_INPUT) { string defaultValue = String.Empty; if (!String.IsNullOrEmpty(upperExtendedData) && dicVariables.ContainsKey(upperExtendedData)) { defaultValue = dicVariables[upperExtendedData]; } using InputBox input = new InputBox("Variable Input", $"Enter value for \"{upperExtendedData}\":", defaultValue); input.ShowDialog(); // Value exists (cancel button was NOT pressed) if (!string.IsNullOrEmpty(input.Input)) { dicVariables[upperExtendedData] = input.Input; } } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_OUTPUT) { if (dicVariables.ContainsKey(upperExtendedData)) { SuperMacroWriter textWriter = new SuperMacroWriter(connection); textWriter.SendInput(dicVariables[upperExtendedData], settings, null, false); } else { Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Output called for {upperExtendedData} without an Input beforehand"); } } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_UNSETALL) { dicVariables.Clear(); } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_UNSET) { if (string.IsNullOrEmpty(upperExtendedData)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Unset called without variable name"); return; } if (dicVariables.ContainsKey(upperExtendedData)) { dicVariables.Remove(upperExtendedData); } } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET || command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE) { if (string.IsNullOrEmpty(macro.ExtendedData)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Set called without variable name"); return; } var splitData = macro.ExtendedData.Split(new char[] { ':' }, 2); if (splitData.Length != 2) { Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Set called without incorrect extended data: {macro.ExtendedData}"); return; } string varInput = splitData[1]; // Set From File but file doesn't exist if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE && !File.Exists(splitData[1])) { Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable SetFromFile called but file does not exist {splitData[1]}"); return; } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE) // File DOES exist { varInput = File.ReadAllText(splitData[1]); } dicVariables[splitData[0].ToUpperInvariant()] = varInput; } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_CLIPBOARD) { var value = ReadFromClipboard(); // Value exists (cancel button was NOT pressed) if (!string.IsNullOrEmpty(value)) { dicVariables[upperExtendedData] = value; } } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_OUTPUT_TO_FILE) { if (string.IsNullOrEmpty(macro.ExtendedData)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile called without any params"); return; } var splitData = macro.ExtendedData.Split(new char[] { ':' }, 2); if (splitData.Length != 2) { Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile called without incorrect extended data: {macro.ExtendedData}"); return; } string variableName = splitData[0].ToUpperInvariant(); string fileName = splitData[1]; // Check if variable exists if (!dicVariables.ContainsKey(variableName)) { Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile called without non existing variable: {variableName}"); return; } // Try to save the data in the variable to the filename try { File.WriteAllText(fileName, dicVariables[variableName]); } catch (Exception ex) { Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile exception: {macro.ExtendedData} {ex}"); } return; } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_RUN) { try { upperExtendedData = upperExtendedData.ToLower(); upperExtendedData = upperExtendedData.Trim(); int pos = upperExtendedData.IndexOf(".exe", StringComparison.InvariantCultureIgnoreCase); if (pos == -1) { return; } string exePath = upperExtendedData.Substring(0, pos + 4); string args = ""; if (!upperExtendedData.EndsWith(".exe")) { args = upperExtendedData.Substring(pos + 5, upperExtendedData.Length - (pos + 5)); } if (File.Exists(exePath)) { if (args != "") { Process.Start(exePath, args); } else { Process.Start(exePath.ToLower()); } } return; } catch (Exception ex) { Logger.Instance.LogMessage(TracingLevel.WARN, string.Format("Run exception: {0} {1}", macro.ExtendedData, ex)); return; } } else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_PROFILE) { try { connection.SwitchProfileAsync(upperExtendedData).Wait(); return; } catch (Exception arg2) { Logger.Instance.LogMessage(TracingLevel.WARN, string.Format("SetProfile exception: {0} {1}", macro.ExtendedData, arg2)); return; } } else { Logger.Instance.LogMessage(TracingLevel.ERROR, $"HandleVariableCommand - Invalid command {command}"); } }