472,111 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

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 17549
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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.