Enter key press in C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Detecting Enter key presses in C# differs by application type. In console apps, use Console.ReadKey() and check for ConsoleKey.Enter. In Windows Forms, handle the KeyDown or KeyPress event and check for Keys.Enter. In WPF, handle the KeyDown event and check for Key.Enter. A common use case is submitting a form or triggering a search when the user presses Enter in a text field, without requiring a button click.
Console Applications
Detecting Enter with ReadKey
Console.ReadKey(intercept: true) reads a key without displaying it. The Key property returns a ConsoleKey enum value.
Waiting for Enter in a Loop
Console.ReadLine() blocks until Enter is pressed and returns the entered text (without the Enter character).
Windows Forms
KeyDown Event on a TextBox
e.SuppressKeyPress = true prevents the default "ding" sound that Windows plays when Enter is pressed in a single-line TextBox.
KeyPress Event
AcceptButton (Form-Level Enter Handling)
AcceptButton is the simplest way to handle Enter at the form level — pressing Enter anywhere on the form triggers the specified button's Click event.
Handling Enter in a DataGridView
WPF Applications
KeyDown Event
Using Input Bindings (MVVM)
Preview Events for Tunneling
Use PreviewKeyDown when you need to intercept the key before the control handles it (e.g., preventing the default behavior).
ASP.NET / Blazor
Blazor
ASP.NET MVC (JavaScript)
Modifier Keys with Enter
Detect Enter combined with Ctrl, Shift, or Alt:
Common Pitfalls
- Not suppressing the "ding" sound in Windows Forms: Pressing Enter in a single-line TextBox plays a system beep. Set
e.SuppressKeyPress = trueinKeyDownore.Handled = trueinKeyPressto prevent it. - Using
KeyPressfor special keys: TheKeyPressevent only fires for character keys, not for function keys, arrow keys, or modifier-only presses. UseKeyDownorKeyUpfor non-character keys like Enter, Escape, and Tab. - Confusing
Keys.EnterandKeys.Return: In Windows Forms,Keys.EnterandKeys.Returnhave the same numeric value (13). Either works, butKeys.Enteris more readable. - Not setting
e.Handled = truein WPF: Without marking the event as handled, the Enter key event bubbles up to parent controls, potentially triggering additional handlers or default behaviors. - Using
AcceptButtonwhen different Enter actions are needed per control:AcceptButtonapplies to the entire form. If different text boxes need different Enter behavior, use per-controlKeyDownhandlers instead.
Summary
- Console apps: use
Console.ReadKey()and checkkey.Key == ConsoleKey.Enter - Windows Forms: handle
KeyDownevent, checke.KeyCode == Keys.Enter, sete.SuppressKeyPress = true - WPF: handle
KeyDownevent, checke.Key == Key.Enter, or useKeyBindingin XAML for MVVM - Use
AcceptButtonin Windows Forms for form-level Enter handling - Always suppress the default behavior (
e.Handled,e.SuppressKeyPress) to prevent unwanted side effects
Related reading
- Enter symbol into a text Label in Windows Forms?
- Entity Framework - Cannot convert lambda expression to type 'string' because it is not a delegate type
- Entity Framework - Code First - Can't Store ListString
- Entity Framework - Invalid Column Name '_ID when not using entity connection string
- Entity Framework 4 - AddObject vs Attach
- Entity Framework 4 mapping fragment error when adding new entity scalar
- Entity Framework 4 Single vs First vs FirstOrDefault
- Entity Framework 5 Updating a Record

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.