KeyCode to ASCII code/character

  • Thread starter Thread starter Ronald Dodge
  • Start date Start date
R

Ronald Dodge

Is there a way to convert the KeyCode to ASCII code. Main reason for this,
I can then compare the code to the code I need more easily and still be able
to cancel out the keystroke by changing the KeyCode to '0', should it need
to be cancelled. I already have setup most of my general data validation
codes to be ran through the KeyDown Event. Once converted to the ASCII
code, I can use the Chr function to convert it to character, should I need
to do that.
 
KeyPress event uses KeyAscii, KeyDown uses KeyCode.
Supposedly if you subtract 144 from KeyCode you should get
the ascii code, but I haven't tested that. Look into
using the KeyPress event instead of the event you're using.
 
HI,
This may get you started. It will work for any alpha key. You'll have to
figure out the relationship for the other keys:

If KeyCode <> 16 Then
If Shift = 1 Then
Debug.Print Chr(KeyCode) & KeyCode
Else
Debug.Print Chr(KeyCode + 32) & KeyCode + 32
End If
End If

The first If ......<> 16 is there because you get a KeyCode of 16 when
the shift key is pressed.
 
I don't know if anyone will see this, but I found it so I'll post an update from my experience applying the tips here. In addition to adding 32 if the shift key is used, you need to subtract 144 if KeyCode is over 144.

Thank you for the comments above. With the help of both of the responders, I was able to get some code working that had been giving me a lot of headaches.
 
Back
Top