Support text pasting with Ctrl+V

This commit is contained in:
ORelio 2014-01-11 12:48:59 +01:00
parent d012905b65
commit bca2a4116c
2 changed files with 99 additions and 66 deletions

View file

@ -2,6 +2,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace MinecraftClient namespace MinecraftClient
{ {
@ -80,77 +82,86 @@ namespace MinecraftClient
k = Console.ReadKey(true); k = Console.ReadKey(true);
while (writing_lock) { } while (writing_lock) { }
reading_lock = true; reading_lock = true;
switch (k.Key) if (k.Key == ConsoleKey.V && k.Modifiers == ConsoleModifiers.Control)
{ {
case ConsoleKey.Escape: string clip = ReadClipboard();
ClearLineAndBuffer(); foreach (char c in clip)
break; AddChar(c);
case ConsoleKey.Backspace: }
RemoveOneChar(); else
break; {
case ConsoleKey.Enter: switch (k.Key)
Console.Write('\n'); {
break; case ConsoleKey.Escape:
case ConsoleKey.LeftArrow: ClearLineAndBuffer();
GoLeft(); break;
break; case ConsoleKey.Backspace:
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();
RemoveOneChar(); RemoveOneChar();
} break;
break; case ConsoleKey.Enter:
case ConsoleKey.Oem6: Console.Write('\n');
break; break;
case ConsoleKey.DownArrow: case ConsoleKey.LeftArrow:
if (previous.Count > 0) GoLeft();
{ break;
ClearLineAndBuffer(); case ConsoleKey.RightArrow:
buffer = previous.First.Value; GoRight();
previous.AddLast(buffer); break;
previous.RemoveFirst(); case ConsoleKey.Home:
Console.Write(buffer); while (buffer.Length > 0) { GoLeft(); }
} break;
break; case ConsoleKey.End:
case ConsoleKey.UpArrow: while (buffer2.Length > 0) { GoRight(); }
if (previous.Count > 0) break;
{ case ConsoleKey.Delete:
ClearLineAndBuffer(); if (buffer2.Length > 0)
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)
{ {
string word_tocomplete = tmp[tmp.Length - 1]; GoRight();
string word_autocomplete = autocomplete_engine.AutoComplete(word_tocomplete); RemoveOneChar();
if (!String.IsNullOrEmpty(word_autocomplete) && word_autocomplete != word_tocomplete) }
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(); } string word_tocomplete = tmp[tmp.Length - 1];
foreach (char c in word_autocomplete) { AddChar(c); } 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;
break; default:
default: AddChar(k.KeyChar);
AddChar(k.KeyChar); break;
break; }
} }
reading_lock = false; reading_lock = false;
} }
@ -212,7 +223,7 @@ namespace MinecraftClient
} }
#endregion #endregion
#region subfunctions #region Subfunctions
private static void ClearLineAndBuffer() private static void ClearLineAndBuffer()
{ {
while (buffer2.Length > 0) { GoRight(); } while (buffer2.Length > 0) { GoRight(); }
@ -275,6 +286,27 @@ namespace MinecraftClient
for (int i = 0; i < buffer2.Length; i++) { GoBack(); } for (int i = 0; i < buffer2.Length; i++) { GoBack(); }
} }
#endregion #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
} }
/// <summary> /// <summary>

View file

@ -74,6 +74,7 @@
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />