public void ProcessKeyDown(KeyEventArgs e)
{
if((e.Alt == true) || (e.Control == true))
{
if((e.Control == true) && (e.KeyCode == Keys.A))
{
this.SelectionStart = 0;
this.SelectionDirection = _buffer.Length;
}
}
else if((e.KeyCode == Keys.Return) || (e.KeyCode == Keys.Enter))
{
if(_buffer.Length > 0)
{
if(InputAvailable != null)
{
InputAvailable(this, new EventArgs());
}
_historyLines.Add(_buffer.ToString());
_historyLine = _historyLines.Count;
_buffer.Clear();
_selectionStart = 0;
_selectionDirection = 0;
}
}
else if(e.KeyCode == Keys.Tab)
{
AutoComplete();
return;
}
else if((e.KeyCode == Keys.Home) || (e.KeyCode == Keys.End))
{
if(e.Shift == true)
{
// we're shift selecting while pressing home or end, figure out where to select from.
int start = this.SelectionStart;
this.SelectionStart = (e.KeyCode == Keys.Home) ? 0 : (this.SelectionDirection != 0) ? -this.SelectionDirection : start;
if(e.KeyCode == Keys.End)
{
this.SelectionDirection = _buffer.Length - start;
}
else
{
this.SelectionDirection = -(start + 1);
}
}
else
{
this.SelectionDirection = 0;
this.SelectionStart = (e.KeyCode == Keys.Home) ? 0 : _buffer.Length;
}
}
else if((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.Right))
{
if(e.Shift == true)
{
this.SelectionDirection += (e.KeyCode == Keys.Left) ? -1 : 1;
}
else
{
if(this.SelectionDirection != 0)
{
if((this.SelectionDirection != 0) && (e.KeyCode == Keys.Right))
{
this.SelectionStart = this.SelectionStart + this.SelectionLength;
}
else
{
this.SelectionStart = this.SelectionStart;
}
this.SelectionDirection = 0;
}
else
{
this.SelectionStart += (e.KeyCode == Keys.Left) ? -1 : 1;
}
}
}
else if((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.Down))
{
// command history
_historyLine += (e.KeyCode == Keys.Up) ? -1 : 1;
if(_historyLine < 0)
{
_historyLine = 0;
}
else if(_historyLine >= _historyLines.Count)
{
_historyLine = _historyLines.Count;
}
_buffer.Clear();
if(_historyLine < _historyLines.Count)
{
_buffer.Append(_historyLines[_historyLine]);
}
this.SelectionStart = this.Length;
this.SelectionDirection = 0;
}
else if((e.KeyCode == Keys.Back) || (e.KeyCode == Keys.Delete))
{
if((e.KeyCode == Keys.Delete) && (this.SelectionStart == _buffer.Length))
{
// don't want to delete past the end of the buffer.
return;
}
else if((e.KeyCode == Keys.Back) && (this.SelectionStart == 0))
{
// don't want backspace deleting paste the start of the buffer.
return;
}
else if(this.SelectionLength > 0)
{
_buffer.Remove(this.SelectionStart, this.SelectionLength);
this.SelectionStart = this.SelectionStart;
}
else
{
int dir = (e.KeyCode == Keys.Back) ? -1 : 0;
_buffer.Remove(this.SelectionStart + dir, 1);
this.SelectionStart += dir;
}
if(this.SelectionStart > _buffer.Length)
{
this.SelectionStart = _buffer.Length;
}
this.SelectionDirection = 0;
}
else
{
char c = idHelper.CharacterFromKeyCode(e.KeyCode, e.Modifiers);
if(c != '\0')
{
_buffer.Insert(_selectionStart, c);
this.SelectionStart++;
this.SelectionDirection = 0;
}
}
// pressing tab exits the method above, so if we got here
// the user pressed another key and broke out of autocomplete.
_autoComplete = null;
}