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 Course DevExpress 12.2.4 full for Visual Studio 2010

VB .NET Google Drive Api Source Code Example

Visual Basic Online Course - The Microsoft.ACE.OLEDB.12.0 provider is not registered on the local machine

Visual Basic Compare Two Treeviews and return differences

Visual Basic Online Courses DataGridView Add Edit Delete

Visual Basic Online Course The following control could not be licensed: TxTextControl.TextControl