Click here to Skip to main content
15,918,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to catch an enter key event in c# Windows apllication rather than using a graphical button?
Posted
Comments
Pete O'Hanlon 22-Jan-14 12:06pm    
Are you using Windows Forms or WPF? The solution is different depending on what framework you are coding your app for.

use key down[^] event

C#
private void Form1_KeyDown(object sender, KeyEventArgs e)
       {
           if (e.KeyCode == Keys.Enter)
           {
               MessageBox.Show("Enter key pressed");
           }
       }
 
Share this answer
 
Comments
Rahul VB 22-Jan-14 14:01pm    
5+ "MOM", great. Its like you read my mind, i will not post any thing.
Karthik_Mahalingam 22-Jan-14 20:34pm    
Thanks Rahul :)
Praveen_P 23-Jan-14 1:58am    
5
Karthik_Mahalingam 23-Jan-14 2:05am    
Thanks Praveen :)
If you want to catch the 'Enter KeyDown Event for the entire Form ... before any of the Controls on the Form receive the KeyDown Event:

1. set the Form's KeyPreview Property to 'true

2. wire-up an EventHandler for the Form's KeyDown Event:
C#
private bool IsEnterSeen = false;

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    IsEnterSeen = e.KeyCode == Keys.Enter;

    // take action here if 'IsEnterSeen == 'true

}
If you want to get the 'Enter KeyPress Event for the entire Form, and you want to cancel the further processing of the 'Enter key:
C#
private char enterChar = (char)13;

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    IsEnterSeen = e.KeyChar == enterChar;

    // take action here if 'IsEnterSeen == 'true

    // to cancel further processing of the Enter Key:
    // if(IsEnterSeen) e.Handled = true;

}
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 22-Jan-14 11:21am    
5
Rahul VB 22-Jan-14 14:01pm    
Great sir, 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900