Код: Выделить всё
TreatCtrlCAsInputКод: Выделить всё
public override ConsoleKeyInfo Activate( string value = "" )
{
base.Value = string.IsNullOrEmpty( value ) ? this.Value : value;
this.Active = true;
this.CursorPosition = new( 0, 0 );
bool saveState = Console.TreatControlCAsInput;
while (true)
{
var key = Console.ReadKey( true );
switch (key.Key)
{
case ConsoleKey.DownArrow:
if (this.CursorPosition.Y + 1 >= this.Size.Height) return key;
CursorPosition = new( CursorPosition.X, CursorPosition.Y + 1 );
break;
case ConsoleKey.UpArrow:
if (this.CursorPosition.Y == 0) return key;
CursorPosition = new( CursorPosition.X, CursorPosition.Y - 1 );
break;
case ConsoleKey.RightArrow:
if (this.CurrentIndex < MaxLength)
{
int start = this.CurrentIndex;
if (key.Modifiers.HasFlag( ConsoleModifiers.Control ))
{
int i = this.CurrentIndex;
while (i < Length && this.Value[ i++ ] == ' ') ;
while (i < Length && this.Value[ i++ ] != ' ') ;
this.CurrentIndex = Math.Min( Length, i );
}
else
this.CurrentIndex += 1;
if (key.Modifiers.HasFlag( ConsoleModifiers.Shift ))
this.Selection = (start, this.CurrentIndex - start);
}
break;
case ConsoleKey.LeftArrow:
if (this.CurrentIndex > 0)
{
int start = this.CurrentIndex;
if (key.Modifiers.HasFlag( ConsoleModifiers.Control ))
{
int i = this.CurrentIndex;
while (i > 0 && this.Value[ --i ] == ' ') ;
while (i > 0 && this.Value[ --i ] != ' ') ;
this.CurrentIndex = Math.Max( 0, i + 1 );
}
else
this.CurrentIndex -= 1;
if (key.Modifiers.HasFlag( ConsoleModifiers.Shift ))
this.Selection = (this.CurrentIndex, start - this.CurrentIndex);
}
break;
case ConsoleKey.Home:
if (key.Modifiers.HasFlag( ConsoleModifiers.Control ))
this.CurrentIndex = 0;
else
this.CursorPosition = new( 0, this.CursorPosition.Y );
break;
case ConsoleKey.End:
if (key.Modifiers.HasFlag( ConsoleModifiers.Control ))
this.CurrentIndex = this.Value.Length;
else
this.CursorPosition = new( this.Lines[ this.CursorPosition.Y ].Length, this.CursorPosition.Y );
break;
case ConsoleKey.Backspace:
if (Enabled && this.CurrentIndex > 0)
{
this.CurrentIndex -= 1;
goto Delete;
}
break;
case ConsoleKey.Delete:
Delete:
if (Enabled && SelectedText.Length > 0)
{
if (key.Modifiers.HasFlag( ConsoleModifiers.Shift ))
{
System.Windows.Forms.Clipboard.SetText( this.Value.Substring( this.SelectionStart, this.SelectionLength ) );
SystemSounds.Asterisk.Play();
}
this.Value = this.Value.Remove( this.SelectionStart, this.SelectionLength );
break;
}
if (Enabled && this.CurrentIndex < this.Value.Length)
this.Value = this.Value.Remove( this.CurrentIndex, 1 );
break;
case ConsoleKey.A:
if (!key.Modifiers.HasFlag( ConsoleModifiers.Control )) goto default;
this.Selection = (0, Length);
Draw();
break;
case ConsoleKey.V:
if (key.Modifiers.HasFlag( ConsoleModifiers.Control )) goto Insert;
goto default;
case ConsoleKey.Insert:
if (!key.Modifiers.HasFlag( ConsoleModifiers.Shift ) || !System.Windows.Forms.Clipboard.ContainsText()) break;
Insert:
string s = System.Windows.Forms.Clipboard.GetText();
this.SelectionLength = s.Length;
this.SelectedText = s;
break;
case ConsoleKey.Tab:
case ConsoleKey.Enter:
case ConsoleKey.Escape:
this.Active = false;
Console.TreatControlCAsInput = saveState;
return key;
case ConsoleKey.X: // Cut
if (!key.Modifiers.HasFlag( ConsoleModifiers.Control )) goto default;
if (this.SelectionLength > 0)
{
System.Windows.Forms.Clipboard.SetText( this.SelectedText );
SystemSounds.Asterisk.Play();
this.SelectedText = "";
}
break;
case ConsoleKey.C:
if (!key.Modifiers.HasFlag( ConsoleModifiers.Control )) goto default;
if (this.SelectionLength > 0)
{
System.Windows.Forms.Clipboard.SetText( this.SelectedText );
SystemSounds.Asterisk.Play();
}
break;
default:
if (Length < MaxLength && RxFilter.IsMatch( $"{key.KeyChar}" ))
{
this.Value = this.Value.Insert( this.CurrentIndex, $"{key.KeyChar}" );
this.CurrentIndex += 1;
}
else
SystemSounds.Beep.Play();
break;
}
};
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... -right-etc
Мобильная версия