Author

Ahmed Samir

Evry1falls is my online name

KeyPress vs KeyDown in VB.Net

KeyPress vs KeyDown in VB.Net 5 of 5
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.

Comment Via facebook

Popular posts from this blog

Visual Basic 2010 Read/Write Command Prompt

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

Visual Basic Online Course DevExpress 12.2.4 full for Visual Studio 2010

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