KeyPress vs KeyDown in VB.Net
Keypress and Keydown are both used whenever a keyboard button is pressed down, but what is the difference between both of them and when should you use which one of them ?
Keypress : occurs in between both "Keydown" and "Keyup" events.
example : If you want to exit a form using "Escape" from keyboard buttons.
Keypress vs Keydown events in VB.Net |
Notice keydown occurs first when I pressed "Escape" button. |
Now, let's try pressing another button in the keyboard "i.e: N" and instead of Close() we will generate msgbox ("Keydown") and msgbox ("Keypress")
Keypress VS Keydown sequence test. |
When I press "N" naturally Keydown occurs first then Keypress event and the application will show a msgbox ("Keydown") only but only after Keypress occurs.
Conclusion: Keypress occurs after keydown event and before keyup event. and also another major difference is there are some buttons in the keyboard that won't work directly with keypress like :
- Shift, Ctrl, Alt
- F1 through F12
- Arrow keys.