From bca2a4116c9b4f171441ff11c7d1eba989565ab0 Mon Sep 17 00:00:00 2001 From: ORelio Date: Sat, 11 Jan 2014 12:48:59 +0100 Subject: [PATCH] Support text pasting with Ctrl+V --- MinecraftClient/ConsoleIO.cs | 164 +++++++++++++++---------- MinecraftClient/MinecraftClient.csproj | 1 + 2 files changed, 99 insertions(+), 66 deletions(-) diff --git a/MinecraftClient/ConsoleIO.cs b/MinecraftClient/ConsoleIO.cs index b01f1137..25bbacce 100644 --- a/MinecraftClient/ConsoleIO.cs +++ b/MinecraftClient/ConsoleIO.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Windows.Forms; +using System.Threading; namespace MinecraftClient { @@ -80,77 +82,86 @@ namespace MinecraftClient k = Console.ReadKey(true); while (writing_lock) { } reading_lock = true; - switch (k.Key) + if (k.Key == ConsoleKey.V && k.Modifiers == ConsoleModifiers.Control) { - case ConsoleKey.Escape: - ClearLineAndBuffer(); - break; - case ConsoleKey.Backspace: - RemoveOneChar(); - break; - case ConsoleKey.Enter: - Console.Write('\n'); - break; - case ConsoleKey.LeftArrow: - GoLeft(); - break; - case ConsoleKey.RightArrow: - GoRight(); - break; - case ConsoleKey.Home: - while (buffer.Length > 0) { GoLeft(); } - break; - case ConsoleKey.End: - while (buffer2.Length > 0) { GoRight(); } - break; - case ConsoleKey.Delete: - if (buffer2.Length > 0) - { - GoRight(); + string clip = ReadClipboard(); + foreach (char c in clip) + AddChar(c); + } + else + { + switch (k.Key) + { + case ConsoleKey.Escape: + ClearLineAndBuffer(); + break; + case ConsoleKey.Backspace: RemoveOneChar(); - } - break; - case ConsoleKey.Oem6: - break; - case ConsoleKey.DownArrow: - if (previous.Count > 0) - { - ClearLineAndBuffer(); - buffer = previous.First.Value; - previous.AddLast(buffer); - previous.RemoveFirst(); - Console.Write(buffer); - } - break; - case ConsoleKey.UpArrow: - if (previous.Count > 0) - { - ClearLineAndBuffer(); - buffer = previous.Last.Value; - previous.AddFirst(buffer); - previous.RemoveLast(); - Console.Write(buffer); - } - break; - case ConsoleKey.Tab: - if (autocomplete_engine != null && buffer.Length > 0) - { - string[] tmp = buffer.Split(' '); - if (tmp.Length > 0) + break; + case ConsoleKey.Enter: + Console.Write('\n'); + break; + case ConsoleKey.LeftArrow: + GoLeft(); + break; + case ConsoleKey.RightArrow: + GoRight(); + break; + case ConsoleKey.Home: + while (buffer.Length > 0) { GoLeft(); } + break; + case ConsoleKey.End: + while (buffer2.Length > 0) { GoRight(); } + break; + case ConsoleKey.Delete: + if (buffer2.Length > 0) { - string word_tocomplete = tmp[tmp.Length - 1]; - string word_autocomplete = autocomplete_engine.AutoComplete(word_tocomplete); - if (!String.IsNullOrEmpty(word_autocomplete) && word_autocomplete != word_tocomplete) + GoRight(); + RemoveOneChar(); + } + break; + case ConsoleKey.Oem6: + break; + case ConsoleKey.DownArrow: + if (previous.Count > 0) + { + ClearLineAndBuffer(); + buffer = previous.First.Value; + previous.AddLast(buffer); + previous.RemoveFirst(); + Console.Write(buffer); + } + break; + case ConsoleKey.UpArrow: + if (previous.Count > 0) + { + ClearLineAndBuffer(); + buffer = previous.Last.Value; + previous.AddFirst(buffer); + previous.RemoveLast(); + Console.Write(buffer); + } + break; + case ConsoleKey.Tab: + if (autocomplete_engine != null && buffer.Length > 0) + { + string[] tmp = buffer.Split(' '); + if (tmp.Length > 0) { - while (buffer.Length > 0 && buffer[buffer.Length - 1] != ' ') { RemoveOneChar(); } - foreach (char c in word_autocomplete) { AddChar(c); } + string word_tocomplete = tmp[tmp.Length - 1]; + string word_autocomplete = autocomplete_engine.AutoComplete(word_tocomplete); + if (!String.IsNullOrEmpty(word_autocomplete) && word_autocomplete != word_tocomplete) + { + while (buffer.Length > 0 && buffer[buffer.Length - 1] != ' ') { RemoveOneChar(); } + foreach (char c in word_autocomplete) { AddChar(c); } + } } } - } - break; - default: - AddChar(k.KeyChar); - break; + break; + default: + AddChar(k.KeyChar); + break; + } } reading_lock = false; } @@ -212,7 +223,7 @@ namespace MinecraftClient } #endregion - #region subfunctions + #region Subfunctions private static void ClearLineAndBuffer() { while (buffer2.Length > 0) { GoRight(); } @@ -275,6 +286,27 @@ namespace MinecraftClient for (int i = 0; i < buffer2.Length; i++) { GoBack(); } } #endregion + + #region Clipboard management + private static string ReadClipboard() + { + string clipdata = ""; + Thread staThread = new Thread(new ThreadStart( + delegate + { + try + { + clipdata = Clipboard.GetText(); + } + catch { } + } + )); + staThread.SetApartmentState(ApartmentState.STA); + staThread.Start(); + staThread.Join(); + return clipdata; + } + #endregion } /// diff --git a/MinecraftClient/MinecraftClient.csproj b/MinecraftClient/MinecraftClient.csproj index 78e2c47e..48af6f73 100644 --- a/MinecraftClient/MinecraftClient.csproj +++ b/MinecraftClient/MinecraftClient.csproj @@ -74,6 +74,7 @@ +