# Arrow key control on winforms RichTextBox

I am making a faux console using winforms for the game I am working on. Right now I am moving a '0' across the screen using the arrow keys.

In order to provide fine grain formatting on the text, I am using a richTextBox for the console display. The background for this box is black.

Problem: If the richTextBox is set to Enabled = false I can use the arrow keys without a problem but the box background color is grey and will not change. If I set it to read-only, when using the arrow keys it will just move the cursor and my arrow key events will not fire.

I am fine with setting the text to read-only and getting my black control color. So my question would be, is there a way to set the control to read-only and still allow for my arrow key events (and not move the cursor)?

• This is a good question that has research value for stack exchange but I am voting to move it to stack overflow as it is not game development specific. – lozzajp Nov 1 '16 at 8:51

As Ausa mentioned, you can use KeyPreview and SupressKeyPress.

public MainForm()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(MainForm_KeyDown);
}

void MainForm_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true; // cancel key press
if (e.KeyCode == Keys.Right) // do whatever you want with your user input
{
MoveRight(); // for example
}
}


One way could be to make it so whenever the text box got focus (watch for the event), set the focus to a different control instead (maybe the main window).

In that other control, you could watch for arrow key events and act on them by running the code that does whatever you want to do to the contents of the rich text box.

To the user, things would seem like you want them to, even though in reality there are a few more moving parts.

If you're raising the key press event from your main form then you should be able to simply set the KeyPreview property to true on your main form and it will register the key press first, befor the Rich Text Box does.