KeyDown and KeyCodes

Joshann

Registered User.
Local time
Today, 09:37
Joined
Mar 22, 2002
Messages
142
I am using the KeyDown event and am trying to determine if the key pressed is any kind of character or number (as opposed to keys like Tab, Ctrl, Print Screen, etc.). There must be an easy way to do this, but I've looked everywhere, and all I can come up with is something like the following:
Select Case KeyCode
Case vbKey0 to vbKey9
IsCharacter = True
Case vbKeyA to vbKeyZ
IsCharacter = True
Case vbKeyDecimal, vbKeyDivide, vbKeyMultiply
IsCharacter = True
.
.
.
And so on. There must be an easier way to do this than listing all the characters on the keyboard. Can anyone help?
 
Code:
Dim strChar As String
strChar = UCase(Chr(KeyCode))
If CInt(strChar) < 65 Or CInt(strChar) > 90 Then
    MsgBox "Is not alphabetical...."
End If

Sorry, I have misread your problem, thinking you wanted to test for alphabetical characters over numbers and special keys. :rolleyes:

Rather than list all those you don't care about, why not just check for the ones you want to know about?

i.e.

Code:
IsCharacter = True
If KeyCode = vbTab, vbBack Then
    IsCharacter = False
End If


I think, however, that all special characters' ASCII codes are below 32 (0-31)

So,

Code:
If Asc(KeyCode) < 32 Then

might just do the trick. :cool:
 
Last edited:
Mile-O-Phile said:
Code:
Dim strChar As String
strChar = UCase(Chr(KeyCode))
If CInt(strChar) < 65 Or CInt(strChar) > 90 Then
    MsgBox "Is not alphabetical...."
End If
That works for the letters, but what about the numbers, the decimal point, the colon, the apostrophe, and so on and so on?
 
Read above; I edited the post. :rolleyes:
 
Mile-O-Phile said:
Read above; I edited the post. :rolleyes:
Thanks. The problem is that apparently the Keycode does not correspond to the ASCII character set. For example, try putting

MsgBox Asc(KeyCode)

in the KeyDown event on a form. Then press the letter a. You should get 65, but instead you get 54. The problem is that I don't just want to know if a letter was entered, I want to know if a letter, number, or any character (the decimal key, the colon, an apostrophe, etc.) was pressed.
 
Sorry, it's late:

Code:
If KeyCode < 32 Then
 
Mile-O-Phile said:
Sorry, it's late:

Code:
If KeyCode < 32 Then
So all the KeyCodes for keys like Tab, Insert, Home, etc. are below 32?
 
Joshann said:
So all the KeyCodes for keys like Tab, Insert, Home, etc. are below 32?

Yes. If you don't believe, open Excel, put 0-255 in a column. Set the formula to each one to =CHAR(cell) and try it for yourself. ;)
 
Mile-O-Phile said:
Yes. If you don't believe, open Excel, put 0-255 in a column. Set the formula to each one to =CHAR(cell) and try it for yourself. ;)
Unfortunately, Access must be using different codes than Excel. For example, the KeyCode for the Insert key is 45, and the KeyCode for Home is 36. You can check this by putting

MsgBox KeyCode

in the KeyDown event on a form. Then press the Insert key on your keyboard. You'll get 45. That's O.K., I'll just list each and every KeyCode for all the keys that I don't want. I was just looking for an easier way.

Thanks!
 

Users who are viewing this thread

Back
Top Bottom