If you need to respond to Enter key press, use different event:
this.textBox.KeyDown += (sender, eventArgs) => {
if (eventArgs.KeyCode == Keys.Enter;
};
You could use the event
KeyUp
the same way, not not very different event
KeyPress
which really works with characters and is, importantly, cancellable (and this is the way to filter some characters from input).
Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown%28v=vs.110%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs%28v=vs.110%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keycode%28v=vs.110%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keys%28v=vs.110%29.aspx[
^].
You need to understand that keys presses are lower level then characters (scan codes are even low level), and many do not correspond to any, others depends on current thread culture and current input method and input language chosen for a current application. If you need to handle the press of a key, do just this, don't try to type cast it to some character. Besides, the characters created by pressing Enter (say, in
TextBox
), depend on the platform (and CLR is actually multi-platform, not just Microsoft's):
http://en.wikipedia.org/wiki/End_of_line[
^].
You also need to understand that in event-oriented programming nothing is "checked", instead, you are handling the event, and, in this case, the event triggered by hardware interrupt.
—SA