Windows.forms.keys collection

O

Oguzhan Filizlibay

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.

thanks,

as an example:

Dim strCaseID As String = "s"


For Each c As Char In strCaseID
Dim key As UInteger = Convert.ToInt16(c)
DoKeyBoard(0, key)
DoKeyBoard(NativeMethods.KEYEVENTF.KEYUP, key)

Next


in this example s is somehow translated into 115 and this translates to
Keys.F4, which actually should be an 83 to be indicated right..
 
B

Brian Schwartz

The key code for a lower case 's' is 115, but the Keys enumeration does not
make a distinction between lower and upper case. You have two options:
1) convert all alphabetic characters to upper case before converting to the
Keys enum. 's' would become 'S' which would give you code 83 as you expect.
2) keep track of which characters are upper and lower case, and set the
shift key in the Keys enumeration along with the alphabetic character; note
that with this route you still have to convert the letter to upper case:
Dim key As UInteger
If (Char.IsUpper(c)) Then
key = Convert.ToInt16(c) Or Keys.Shift
Else
key = Convert.ToInt16(Char.ToUpper(c))
End If
This all assumes that you're dealing strictly with alphabetic characters. I
didn't test to see how it would respond to symbols and numbers.
 
P

Peter Duniho

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top