void richTextBoxConsole_KeyDown(object sender, KeyEventArgs e)
{
// Are we sending keyboard commands to the process?
if(SendKeyboardCommandsToProcess && IsProcessRunning) {
// Get key mappings for this key event?
var mappings = from k in keyMappings
where
(k.KeyCode == e.KeyCode &&
k.IsAltPressed == e.Alt &&
k.IsControlPressed == e.Control &&
k.IsShiftPressed == e.Shift)
select k;
// Go through each mapping, send the message.
//foreach(var mapping in mappings) {
//SendKeysEx.SendKeys(CurrentProcessHwnd, mapping.SendKeysMapping);
//inputWriter.WriteLine(mapping.StreamMapping);
//WriteInput("\x3", Color.White, false);
//}
// If we handled a mapping, we're done here.
if(mappings.Any()) {
e.SuppressKeyPress = true;
return;
}
}
// If we're at the input point and it's backspace, bail.
if((richTextBoxConsole.SelectionStart <= InputStart) && e.KeyCode == Keys.Back) e.SuppressKeyPress = true;
// Are we in the read-only zone?
if(richTextBoxConsole.SelectionStart < InputStart) {
// Allow arrows and Ctrl-C.
if(!(e.KeyCode == Keys.Left ||
e.KeyCode == Keys.Right ||
e.KeyCode == Keys.Up ||
e.KeyCode == Keys.Down ||
(e.KeyCode == Keys.C && e.Control))) {
e.SuppressKeyPress = true;
}
} else {
if(e.KeyCode == Keys.Up) {
if(HistoryIndex >= 0) {
HistoryIndex--;
}
ScrollHistory();
e.SuppressKeyPress = true;
} else if(e.KeyCode == Keys.Down) {
if(HistoryIndex < CommandHistory.Count) {
HistoryIndex++;
}
ScrollHistory();
e.SuppressKeyPress = true;
}
}
// Is it the return key?
if(e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return) {
// Get the input.
// move to the end...
string input = richTextBoxConsole.Text.Substring(InputStart, (richTextBoxConsole.SelectionStart) - InputStart);
if(!string.IsNullOrWhiteSpace(input)) {
richTextBoxConsole.Select(richTextBoxConsole.Text.Length, 0);
// Write the input (without echoing).
WriteInput(input, DefaultForeColor, false);
CommandHistory.Add(input);
HistoryIndex = 0;
} else {
// nothing there, ignore?
e.SuppressKeyPress = true;
}
}
ShowHideCaret();
}