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
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top