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,505
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,082,177
Messages
5,363,591
Members
400,753
Latest member
Lizanz

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top