Net-informations.com
SiteMap  | About    

keyPress event in C#

Handle Keyboard Input at the Form Level in C#

c# keypress event

Windows Forms processes keyboard input by raising keyboard events in response to Windows messages. Most Windows Forms applications process keyboard input exclusively by handling the keyboard events.

How do I detect keys pressed in C#

You can detect most physical key presses by handling the KeyDown or KeyUp events. Key events occur in the following order:

KeyDown KeyPress KeyUp

How to detect when the Enter Key Pressed in C#

The following C# code behind creates the KeyDown event handler. If the key that is pressed is the Enter key, a MessegeBox will displayed .

  if (e.KeyCode == Keys.Enter)
  {
	MessageBox.Show("Enter Key Pressed ");
  }

How to get TextBox1_KeyDown event in your C# source file ?

Select your TextBox control on your Form and go to Properties window. Select Event icon on the properties window and scroll down and find the KeyDown event from the list and double click the Keydown Event. The you will get the KeyDown event in your source code editor.

  private void textBox1_KeyDown(.....)
  {
  }
C# key-down

Difference between the KeyDown Event, KeyPress Event and KeyUp Event

KeyDown Event : This event raised as soon as the user presses a key on the keyboard, it repeats while the user keeps the key depressed.

KeyPress Event : This event is raised for character keys while the key is pressed and then released. This event is not raised by noncharacter keys, unlike KeyDown and KeyUp, which are also raised for noncharacter keys

KeyUp Event : This event is raised after the user releases a key on the keyboard.

KeyPress Event :

using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { MessageBox.Show("Enter key pressed"); } if (e.KeyChar == 13) { MessageBox.Show("Enter key pressed"); } } } }

KeyDown Event :

using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { MessageBox.Show("Enter key pressed"); } } } }

How to Detecting arrow keys in C#

Detecting arrow keys in C#

In order to capture keystrokes in a Forms control, you must derive a new class that is based on the class of the control that you want, and you override the ProcessCmdKey().

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { //handle your keys here }

More about.... Detecting arrow keys from C#

KeyUp Event :

The following C# source code shows how to capture Enter KeyDown event from a TextBox Control.




using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                MessageBox.Show("Enter key pressed");
            }
        }
    }
}




net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.