Lesson 38: Keyboard Handling in VB6

Master keyboard events to create responsive VB6 applications

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:

  1. KeyDown - When key is first pressed
  2. KeyPress - When character is generated
  3. 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:

Form_KeyPress.vb
Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then ' 13 is ASCII for Enter
        Print "You pressed the Enter key"
    Else
        Print "You pressed: " & Chr(KeyAscii)
    End If
End Sub
                        

Example 38.2: Displaying Pressed Keys

This simple handler shows each key pressed:

Form_KeyPress.vb
Private Sub Form_KeyPress(KeyAscii As Integer)
    Print Chr(KeyAscii) ' Convert ASCII to character
End Sub
                        

Example 38.3: Using Key Constants

Using constants makes your code more readable:

Form_KeyDown.vb
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyF1 Then ' Show help when F1 is pressed
        MsgBox "Help information goes here", vbInformation
    End If
    
    If KeyCode = vbKeyEscape Then ' Exit on Escape
        Unload Me
    End If
End Sub
                        

Example 38.4: Detecting Modifier Keys

The Shift parameter lets you detect Ctrl, Alt, and Shift combinations:

Form_KeyDown.vb
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    ' Check for Ctrl+S combination
    If KeyCode = vbKeyS And Shift = vbCtrlMask Then
        SaveFile
        ' Prevent default behavior
        KeyCode = 0
    End If
    
    ' Check for Alt+F4 combination
    If KeyCode = vbKeyF4 And Shift = vbAltMask Then
        Unload Me
        KeyCode = 0
    End If
End Sub
                        

Common Modifier Keys

Ctrl
Alt
Shift

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

VB6 Key Constants Reference

Complete list of VB6 key constants

View Resource

Keyboard Shortcuts Guide

Implementing common Windows shortcuts

View Resource

Input Validation Techniques

Using keyboard events for data validation

View Resource

Game Development

Keyboard handling for games

View Guide