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
When I run my application and click "escape" button in my keyboard, the first break-point occurs in Keydown event because keydown event is before keypress event, also notice that the application closes without moving to Keypress event because Keydown event occurs before Keypress event.

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 :

  • ShiftCtrlAlt
  • F1 through F12
  • Arrow keys.

Popular posts from this blog

Visual Basic Online Courses DataGridView Add Edit Delete

Visual Basic 2010 Working With DataBase Full Project Example

VB .NET WebView2 WinForms tips

VB NET Check internet connection

Visual Basic .Net - Button Sound / Music Play

Visual Basic 2010 Math Functions