Key Codes and ASCII Values

The following list includes values for the most common Key Codes (also known as Scan or Virtual Key Codes) used with KeyDown, KeyUp and Screen_KeyPreview events and ASCII/ANSI codes for the first 256 characters (used with KeyPress).

For a full list of Virtual Key Codes see MSDN.

Control Characters

Show

Other Non-Character Keys

Show

NumPad Codes

Show

Characters in the ASCII table

Show

Windows 1252 ANSI Codes

Show

Conversion Code

The following simple but clever bit of code converts virtual key codes to ASCII and comes from this page on Sjouke Hamstra's blog.

' Press shift-key than click mouse

Debug.Show

Trace Chr(VkKeyToAscii(65))

Trace Chr(VkKeyToAscii(Asc("8"))) ' –> *

 

Function VkKeyToAscii(keycode As Int) As Int

// Sjouke Hamstra

Dim sb As String * 4

Static Dim keyboardState(256) As Byte

~GetKeyboardState(ArrayAddr(keyboardState()))

If ToAscii(keycode, 0, ArrayAddr(keyboardState()), sb, 0) == 1

Return Asc(sb)

Else

Return 0

EndIf

EndFunc

{Created by Sjouke Hamstra; Last updated: 15/12/2015 by James Gaite}