VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 5

KeyPress Versus KeyUp and KeyDown in Visual Basic 6

KeyDown and KeyUp don't detect exactly the same information as KeyPress.

KeyPress detects a character in its KeyAscii parameter, but KeyDown and KeyUp detect a physical keystroke in their KeyCode parameter. While the difference may seem subtle, it's a difference with practical consequences:

  • KeyUp and KeyDown can detect keystrokes not recognized by KeyPress, such as function, editing, and navigation keys.

  • KeyPress can distinguish between different ASCII characters generated by the same physical keystroke. For instance, KeyPress' KeyAscii parameter gives two different values for "A" and "a." On the other hand, KeyUp and KeyDown's KeyCode parameter gives only one value for these two characters since the physical keystroke is the same (Of course with a little extra logic, you could use the Shift parameter to figure out whether the Shift key were being held). In addition, different countryspecific keyboard mappings will generate difference ASCII codes for the same physical key.

To summarize,

  • You should use KeyDown or KeyUp when you need to detect keystrokes that don't necessarily have an ASCII representation, such as the function keys or the arrow keys, or whenever you are interested in the physical keystroke as such.

  • When you're interested in the actual character that was generated by the keystroke, you should use KeyPress.

NOTE: The Caps Lock Key The Caps Lock key generates its own KeyDown and KeyUp events.

Keystroke Events at Field and Form Level topics

See Also


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved