Re: Windows.forms.keys collection
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Mon, 14 May 2007 11:09:31 -0700
On Sat, 12 May 2007 16:05:02 -0700, Oguzhan Filizlibay <someemail@xxxxxxxxxxxxxx> wrote:
Hi all,
I have the below code for sending each character in a string as a Keys.x enumeration. I've found that when I convert the char, it does not match the enumeration item index and thereby generates a very random output. Any ideas on how I can convert chars to their right equivalent in the Keys enumeration.
If you are going to do that, you need to correctly map the characters to key press events. The code you posted doesn't do that (obviously :) ).
The "Keys" enumeration is for actual keys on the keyboard. They correspond to physical keys, not ASCII characters. It is, in some sense, simply coincidental that a key value corresponds to any ASCII value (though obviously in another sense, that's by design).
Provided you are dealing only with alphanumeric characters, you can use code like what Brian suggests (his code looks to me as though it will handle the digits fine as well...Char.ToUpper() will get called for numbers, but should leave the character unchanged). I think it's safe to assume that the Keys enumeration isn't going to change the assignments for those values. But there are other keys in the enumeration that don't correspond to any ASCII character, and other ASCII characters cannot be converted simply by changing the case and applying a modifier key.
If you want to handle other characters, you may find it better to just use a table-lookup. You can build a Dictionary using ASCII values as the keys, and Keys values with the key code and modifiers pre-combined as the values. You can even populate the Dictionary for the alphanumerics with some simple loops that handle the numbers, upper case, and lower case characters, adding the other data in a more explicit manner (with a hard-coded array containing the desired key/value pairs, or storing them in a resource, or in the applications Settings as an "Application" domain setting).
Then for each character you want to convert, you just look it up in the Dictionary and use the resulting value.
Hope that helps.
Pete
.
- References:
- Windows.forms.keys collection
- From: Oguzhan Filizlibay
- Windows.forms.keys collection
- Prev by Date: Re: Creating directories on Vista machines using .NET
- Next by Date: How to take an IE rendered screenshot of a website with visual studio .net 2002 or visual stuido .net 2003? I can't install visual studio .net 2005 on this computer.
- Previous by thread: Re: Windows.forms.keys collection
- Next by thread: Re: newsgroup for VSTO 2005
- Index(es):
Relevant Pages
|