Keyboard events in VB.NET.
- Keyboard events allow you to validate keystrokes.
- They are raised by the control that has the focus and is receiving input.
Following are the key events:1. KeyDown
2. KeyUp
3. KeyPress
- This allows you to detect when a key on the keyboard was held down.
- This useful for things like validating text in a textbox.
1.
KeyDown:- It is raised as soon as the person presses a key on the keyboard, it repeats while the user holds the key depressed.
- It occurs when a key is pressed down and the control has focus.
Example:Private Sub txtbox1_KeyDown(sender As Object, e As KeyEventAgrs) Handles txtbox1.KeyDown
End Sub
2.
KeyUp:- It is raised after the person releases a key on the keyboard.
- It occurs when a key is released while the control has focus.
Example:Private Sub txtbox1_KeyUp(sender As Object, e As KeyEventAgrs) Handles txtbox1.KeyDown
End Sub
- The KeyDown and KeyUp events get an argument using KeyEventArgs. It has the following properties:
1. Handled : It indicates if the KeyPress event is handled.
2. KeyChar : It stores the character corresponding to the pressed.
3.
KeyPress:- It is raised for character keys while the key is pressed and then released by the user.
- The KeyPress event is not raised by noncharacter keys.
- It occurs when a key is released while the control has focus.
Example:Private Sub txtbox1_KeyPress(sender As Object, e As KeyEventAgrs) Handles txtbox1.KeyDown
End Sub