469,271 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

C# KeyEventArgs - how to quickly determine if letter/number presse

Is there a way to quickly determine if the key pressed was from 'A'-'Z' or
'0'-'9' range?

I really don't like to write switch with every Keys.A, Keys.B, etc key listed

Paul
Feb 7 '07 #1
3 17261
Why don't you convert the pressed key into ASCII representation and then
verify if it matches?

--
Regards,
Robson Siqueira
Enterprise Architect
"panpawel" <pa******@discussions.microsoft.comwrote in message
news:5F**********************************@microsof t.com...
Is there a way to quickly determine if the key pressed was from 'A'-'Z' or
'0'-'9' range?

I really don't like to write switch with every Keys.A, Keys.B, etc key
listed

Paul

Feb 7 '07 #2
Thanks, but to do that I need to get the Char pressed. The pressed key is an
enumerated type Keys (from KeyEventArgs). I could not find a method to
convert it to Char.

Help, please...
"Robson Siqueira" wrote:
Why don't you convert the pressed key into ASCII representation and then
verify if it matches?

--
Regards,
Robson Siqueira
Enterprise Architect
"panpawel" <pa******@discussions.microsoft.comwrote in message
news:5F**********************************@microsof t.com...
Is there a way to quickly determine if the key pressed was from 'A'-'Z' or
'0'-'9' range?

I really don't like to write switch with every Keys.A, Keys.B, etc key
listed

Paul


Feb 8 '07 #3
OK, here is how I did it:

if ( ( ( e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z ) ||
( e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 ) ||
( e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9 ) )

However that works on the assumption than Keys enumeration of A-Z and 0-9
are in the continuous range, which makes sense but is not spelled out
anywhere...

"panpawel" wrote:
Thanks, but to do that I need to get the Char pressed. The pressed key is an
enumerated type Keys (from KeyEventArgs). I could not find a method to
convert it to Char.

Help, please...
"Robson Siqueira" wrote:
Why don't you convert the pressed key into ASCII representation and then
verify if it matches?

--
Regards,
Robson Siqueira
Enterprise Architect
"panpawel" <pa******@discussions.microsoft.comwrote in message
news:5F**********************************@microsof t.com...
Is there a way to quickly determine if the key pressed was from 'A'-'Z' or
'0'-'9' range?
>
I really don't like to write switch with every Keys.A, Keys.B, etc key
listed
>
Paul
Feb 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Adam | last post: by
1 post views Thread by Chaoxiang | last post: by
4 posts views Thread by darrel | last post: by
9 posts views Thread by Ulterior | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.