public static void WriteConsole(string data) { using (SafeHandle hConsoleInput = ConsoleInterface.getConsoleInputHandle()) { // we need 2x the number of characters because we need to simulate key press and key release events for each character NativeInterface.INPUT_RECORD[] inputRecords = new NativeInterface.INPUT_RECORD[data.Length * 2]; for (int i = 0; i < data.Length; i++) { NativeInterface.KEY_EVENT_RECORD keyPressEvent = default(NativeInterface.KEY_EVENT_RECORD); keyPressEvent.bKeyDown = true; keyPressEvent.wRepeatCount = 1; keyPressEvent.UnicodeChar = data[i]; NativeInterface.KEY_EVENT_RECORD keyReleaseEvent = keyPressEvent; // same values for all fields as the key press event, but with the bKeyDown field as "false" keyReleaseEvent.bKeyDown = false; inputRecords[i * 2].EventType = NativeInterface.KEY_EVENT; inputRecords[i * 2].Event = keyPressEvent; inputRecords[i * 2 + 1].EventType = NativeInterface.KEY_EVENT; inputRecords[i * 2 + 1].Event = keyReleaseEvent; } int recordsWritten; ConsoleInterface.callWin32Func(() => NativeInterface.WriteConsoleInput(hConsoleInput, inputRecords, inputRecords.Length, out recordsWritten)); } }