What’s the nicest way to detect only alphanumeric (international) and symbol keystrokes on a KeyDown-event?
A Simple If - Else - End If block based on the Key’s ASCII value would probably be the most manageable.
If Asc(Key) < 127 And Asc(Key) > 32 Then
// It's valid
Return False
Else
// it's invalid
Return True // This basically throws the key away
End If
Of course, include any special case keys in the test.
Perhaps convert the key to ASCII encoding first. Doing that will convert something like “ü” to “u”.
Thanks Tim & Kem!
The Kems advice seems very appropriate to me, just be aware that converting characters that cant be represented in ASCII you get a ? (question mark) as the result of conversion.
What could those characters be?
Example:
dim s as string = ""
dim a as string = ConvertEncoding(s, encodings.ASCII)
a will contain a question mark.
Right, so do something like this:
dim converted as string = s.ConvertEncoding( Encodings.ASCII )
if converted.Asc >= 32 and ( converted <> "?" or s = "?" ) then
// Good character
end if
Thanks! Though, I think I need the “Asc(Key) < 127” part to disregard the Del-key.
Oh, right, and 28-31 for the arrow keys, I think, and 13 for the return, 9 for tab, and some other stuff. I’ve done something like this and it took a bit to get it right.