Welcome to ADO.NET Access 2003—your ultimate hub for VB.NET and ADO.NET programming excellence. Discover in-depth tutorials, practical code samples, and expert troubleshooting guides covering a broad range of topics—from building robust WinForms applications and seamless MS Access integration to working with SQL Server, MySQL, and advanced tools like WebView2 and Crystal Reports. Whether you're a beginner or a seasoned developer, our step-by-step articles are designed to empower you to optimize.

Looking for MS Access Developer❓❓

Application developer

Post Page Advertisement [Top]

Visual Basic Events

KeyPress vs KeyDown

photo of button VBnET Events KeyPress vs KeyDown


Understanding:

If you're developing Windows Forms applications in VB.NET, handling keyboard input correctly is essential. Two common events you’ll encounter are KeyPress and KeyDown.

While both detect when a key is pressed, they behave differently and are used in distinct scenarios. Let’s dive into the difference between KeyPress and KeyDown, with practical examples.

KeyPress and KeyDown are both used whenever a keyboard button is pressed down, but What Is the Difference Between KeyPress and KeyDown in VB.NET? 

KeyDown: 

Occurs when any key is pressed down. It captures both printable and non-printable keys (like arrow keys, function keys, Esc, etc.). 

KeyPress: 

Occurs after KeyDown and is triggered only for character-producing keys (e.g., letters, numbers, and symbols). It doesn’t detect special keys like Escape or Arrow keys.

KeyPress event occurs in between both "KeyDown" and "KeyUp" events.

Real-World Example: Handling the Escape Key

If you want to exit a form using "Escape" from keyboard buttons.

image of IDE VB Net KeyPress vs KeyDown
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.

image of IDE VB Net KeyPress vs KeyDown
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")

image of IDE VB Net KeyPress vs KeyDown
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

  • KeyDown is ideal when detecting any key, including non-printable keys like Escape, Enter, or F1.

  • KeyPress is best for processing typed characters.

  • Order of events: KeyDownKeyPressKeyUp.

Bonus Tip:

Some keys (like Shift, Ctrl, or Arrow keys) are not recognized by the KeyPress event. For full keyboard coverage, always handle the KeyDown event.


  • ShiftCtrlAlt
  • F1 through F12
  • Arrow keys.

     Here are some online Visual Basic lessons and courses:

    No comments:

    Bottom Ad [Post Page]