KeyPress event in VB.NET

In Windows Forms, keyboard input is managed by the framework through the generation of keyboard events in response to Windows messages. When a user interacts with the keyboard, Windows Forms processes the input and raises specific keyboard events, allowing developers to handle and respond to the user's actions.

To effectively process keyboard input in Windows Forms applications, developers typically handle the keyboard events provided by the framework. The two primary keyboard events used for detecting physical key presses are the KeyDown and KeyUp events.

The order in which these key events occur is as follows:

  1. KeyDown: Triggered when a physical key is pressed down.
  2. KeyPress: Raised for character keys after the KeyDown event, providing character input.
  3. KeyUp: Raised when a physical key is released after being pressed down.

KeyDown event

The KeyDown event is triggered when a physical key is pressed down on the keyboard. It provides developers with the opportunity to capture and respond to the key press in real-time. By handling this event, you can perform actions or execute code based on the specific key that was pressed.

KeyPress event

The KeyPress event occurs after the KeyDown event and is raised for character keys. This event provides a way to capture and handle character input from the keyboard. It is useful for scenarios where you need to process specific characters or perform text-related operations.

KeyUp event

The KeyUp event is raised when a physical key is released after being pressed down. It gives developers the ability to respond to key releases and perform actions accordingly. This event is often used in conjunction with the KeyDown event to track the complete lifecycle of a key press, from pressing down to releasing the key.

vb.net keypress event

What it captures:

  1. Printable characters (alphabets, numbers, symbols)
  2. Space key
  3. Backspace key

What it doesn't capture:

  1. Non-printable characters (function keys, arrow keys, Ctrl, Shift, Alt)
  2. Combinations of keys (these are handled by KeyDown and KeyUp events)

How to use it:

  1. Set the Focus: The control (like a text box) needs to have focus for the KeyPress event to work.
  2. Event Handler: In the properties of the control, you can double-click on the KeyPress event to create an event handler function.
  3. Accessing Key Information: Inside the event handler function, you'll have access to the KeyEventArgs object (represented by e in most examples). This object provides properties like: e.KeyChar: This holds the character that was pressed (if applicable).

How to detect when the Enter Key Pressed in VB.NET

The following VB.NET 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 Then MsgBox("enter key pressd ") End If

How to get TextBox1_KeyDown event in your VB.Net source file ?

Select your VB.Net source code view in Visual Studio and select TextBox1 from the top-left Combobox and select KeyDown from top-right combobox , then you will get keydown event sub-routine in your source code editor.

Private Sub TextBox1_KeyDown(...).. End Sub

Difference between the KeyDown Event, KeyPress Event and KeyUp Event in VB.Net

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

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

VB.Net KeyUp Event : This event is raised after the person releases a key on the keyboard.

VB.Net KeyPress Event

Public Class Form1 Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar = Convert.ToChar(13) Then MsgBox("enter key pressd ") End If End Sub End Class

VB.Net KeyDown Event

Public Class Form1 Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Enter Then MsgBox("enter key pressd ") End If End Sub End Class

How to Detecting arrow keys in VB.Net

Detecting arrow keys in VB.Net

In order to capture keystrokes in a VB.Net 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 Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean 'handle your keys here End Function

VB.Net KeyUp Event

The following VB.NET source code shows how to capture Enter KeyUp event from a TextBox Control.

Full Source VB.NET
Public Class Form1 Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp If e.KeyCode = Keys.Enter Then MsgBox("enter key pressd ") End If End Sub End Class

Here are some additional points to consider:

  1. KeyPreview: To capture keypress events even when other controls have focus, set the KeyPreview property of the form to True.
  2. Comparison: KeyPress differs from KeyDown and KeyUp events. KeyDown fires when any key is pressed, KeyUp fires when a key is released, and KeyPress focuses on the character generated by the keypress.

Conclusion

Handling these key events in the appropriate order, you can effectively process and respond to keyboard input within your Windows Forms application. This allows for dynamic and interactive user experiences, where user actions can be captured and appropriate actions can be performed based on the pressed keys.