KeyDown function not returning Ascii values for some key presses

thefordness

New Member
Joined
Oct 23, 2013
Messages
7
I have a routine here that uses KeyDown to get the value of a key press in a userform and use it for a wildcard search in a list box. I refining the code and making changes to accommodate my list and I have discovered that the KeyDown integer values don't always correspond to the ascii table. Chr(Input1) is using the ascii value of the variable to create your search filter as you type. For example: using this code, I get the integer 189 when I press the - key (Hyphen) instead to the ascii value of 45, and the Chr of 189 is the ½ (Fraction one half) which useless for a wildcard search. This works fine for standard alpha characters though. The routine needs to run every time a key is pressed, so I'm not sure if there's a better way to do this other than KeyDown, or if there is a way to get the desired output from KeyDown.
Other example keys that also don't return ascii values: ! " # $ % & ' ( ) * + , - . / etc



To give credit, this was derived from some code found at Ozgrid.com thread #65707


Public Input2 As String
Private Sub ListBox1_KeyDown(ByVal Input1 As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case True
Case Input1 = vbKeyBack
ResetBttn_Click
Case Input1 = vbKeyEscape
Unload Me
Case Input1 = vbKeyReturn
OKBttn_Click
Case (Input1 >= 32 And Input1 <= 126) 'ASCII printable characters (character code 32-127)
Input2 = Input2 & Chr(Input1)
TextBox1.Value = Input2 'this displays the text s you type
FillListBoxByWildChars Input2, [LBTitle], Me.ListBox1
Case Else
End Select
End Sub
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,576
Office Version
2010
Platform
Windows
The KeyDown and KeyUp events use KeyCodes which are the codes used to identify the key on a standard keyboard, not the ASCII code for whatever character is printed on that key... the KeyPress event, on the other hand, does use the ASCII code for the keystroke typed by the user.
 

thefordness

New Member
Joined
Oct 23, 2013
Messages
7
Thanks for the quick reply! I will try my program with the KeyPress event and see how it works!

The KeyDown and KeyUp events use KeyCodes which are the codes used to identify the key on a standard keyboard, not the ASCII code for whatever character is printed on that key... the KeyPress event, on the other hand, does use the ASCII code for the keystroke typed by the user.
 

Forum statistics

Threads
1,086,235
Messages
5,388,624
Members
402,127
Latest member
Jemx

Some videos you may like

This Week's Hot Topics

Top