Lesson 38: Keyboard Handling in VB6
Master keyboard events to create responsive VB6 applications
Lesson Overview
Key Takeaway
Keyboard events allow your VB6 applications to respond to user input beyond mouse clicks, creating more intuitive and efficient user experiences.
Welcome to Lesson 38 of our Visual Basic 6 Tutorial! In this lesson, you'll learn how to handle keyboard events in VB6 using KeyPress, KeyDown, and KeyUp events to create responsive applications that react to user input.
38.1 Understanding Keyboard Events
VB6 provides three keyboard events to handle user input:
KeyPress
Triggers when a key is pressed and released. Best for character input.
KeyDown
Triggers when a key is pressed down. Detects physical key presses.
KeyUp
Triggers when a key is released. Useful for tracking key releases.
KeyDown
Key is pressed
KeyPress
Character is processed
KeyUp
Key is released
Keyboard Event Sequence
When a key is pressed, events fire in this order:
- KeyDown - When key is first pressed
- KeyPress - When character is generated
- KeyUp - When key is released
38.2 ASCII and Key Constants
Keyboard events use ASCII values to identify keys. ASCII (American Standard Code for Information Interchange) assigns numeric values to characters.
Important
Use VB6 key constants (like vbKeyReturn) instead of numeric ASCII values for better code readability.
Common ASCII Values
| Key | ASCII | Constant | Description |
|---|---|---|---|
| Enter | 13 | vbKeyReturn | Carriage return |
| Space | 32 | vbKeySpace | Space bar |
| Escape | 27 | vbKeyEscape | Esc key |
| Tab | 9 | vbKeyTab | Tab key |
| Backspace | 8 | vbKeyBack | Backspace key |
38.3 Writing Keyboard Event Handlers
Let's explore practical examples of keyboard event handling in VB6.
Example 38.1: Detecting the Enter Key
This code detects when the Enter key is pressed:
Private Sub Form_KeyPress(KeyAscii As Integer)If KeyAscii = 13 Then ' 13 is ASCII for EnterPrint "You pressed the Enter key"ElsePrint "You pressed: " & Chr(KeyAscii)End IfEnd Sub
Example 38.2: Displaying Pressed Keys
This simple handler shows each key pressed:
Private Sub Form_KeyPress(KeyAscii As Integer)Print Chr(KeyAscii) ' Convert ASCII to characterEnd Sub
Example 38.3: Using Key Constants
Using constants makes your code more readable:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)If KeyCode = vbKeyF1 Then ' Show help when F1 is pressedMsgBox "Help information goes here", vbInformationEnd IfIf KeyCode = vbKeyEscape Then ' Exit on EscapeUnload MeEnd IfEnd Sub
Example 38.4: Detecting Modifier Keys
The Shift parameter lets you detect Ctrl, Alt, and Shift combinations:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)' Check for Ctrl+S combinationIf KeyCode = vbKeyS And Shift = vbCtrlMask ThenSaveFile' Prevent default behaviorKeyCode = 0End If' Check for Alt+F4 combinationIf KeyCode = vbKeyF4 And Shift = vbAltMask ThenUnload MeKeyCode = 0End IfEnd Sub
Common Modifier Keys
Combine with other keys for powerful keyboard shortcuts
Lesson Summary
In this lesson, you've learned how to handle keyboard events in VB6:
Three Key Events
KeyPress, KeyDown, and KeyUp provide different levels of keyboard interaction
ASCII Values
Numeric codes that represent characters and special keys
Key Constants
Use vbKey constants for readable, maintainable code
Modifier Keys
Detect Ctrl, Alt, and Shift combinations for powerful shortcuts
Pro Tip
For text input controls, use KeyPress. For special keys and modifiers, use KeyDown and KeyUp.
Next Lesson
Continue your VB6 journey with Lesson 39: Printing.
Related Resources
Visual Basic 6 Made Easy
Start your programming journey with Visual Basic 6. Learn how to build Windows applications step-by-step using an easy and beginner-friendly approach.
- Perfect for beginners
- Learn core programming concepts
- Build real VB6 applications
Visual Basic 2026 Made Easy
Upgrade to modern Visual Basic with VB.NET, .NET 10, and Visual Studio 2026. Build real-world applications with modern tools and AI-powered development.
- Modern VB.NET development
- Hands-on projects and real apps
- GitHub Copilot & AI integration
๐ Ready for the Next Level?
After learning the basics with VB6 Made Easy and upgrading to VB2026 Made Easy, continue your journey into advanced professional development.
Advanced VB.NET Programming
Take your VB.NET skills to the next level. Learn advanced programming techniques, real-world architectures, and professional development practices using modern .NET.
Best For:
- After VB6 or VB.NET fundamentals
- Intermediate to advanced learners
- Developers building real-world systems
๐ Migrating from VB6 to VB.NET
Still using or learning classic Visual Basic 6? This practical step-by-step guide shows you how to migrate legacy VB6 applications to modern VB.NET with Visual Studio 2026 and .NET 10.
VB6 to Modern VB.NET Made Easy
A practical step-by-step guide to migrating legacy Visual Basic 6 applications to VB.NET with Visual Studio 2026 and .NET 10.
- Designed for VB6 programmers and legacy app maintainers
- Clear migration guidance from classic VB to modern .NET
- Perfect bridge between VB6 fundamentals and VB.NET development
๐ Move to Modern VB.NET
Visual Basic 6 is your foundation โ but modern development uses VB.NET with .NET and Visual Studio 2026.
Start VB.NET Tutorial โ