Convert A Char To Keys

I have a special char (/ @) that I want to convert to Keys.

I am currently using this:

Keys k = (Keys)'/'; 

And when debugging, I get that k is equal to:

LButton | RButton | MButton | Back | Space Type - System.Windows.Forms.Keys

k keycode should have been 111.

NOTE. The code works for uppercase letters, for example:

 Keys k = (Keys)'Z'; 

In this case, k key code is 90, which is normal.

I am trying to find a way to convert special characters to Keys. (or their corresponding key code)

Trying to send keys globally with:

 public static void SendKey(byte keycode) { const int KEYEVENTF_EXTENDEDKEY = 0x1; const int KEYEVENTF_KEYUP = 0x2; keybd_event(keycode, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0); keybd_event(keycode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0); } SendKey((byte)Keys.{SomethingHere}); 
+7
source share
6 answers

You cannot cast from char to Keys. Keys is a flag enumeration representing which physical keys are pressed on the keyboard. What you are trying may not be an easy problem to solve. Who will say which keyboard the user used and which display generates which character on the user keyboard and their locales?

Can you clarify your purpose? Are you trying to teach the user to enter a special character or something else?

Based on your update, you should use the SendKeys class.

+3
source

This is an old question, but I used this:

 Keys k = (Keys)char.ToUpper(c); 

If the value is char a (with code 97), then the conversion to A (with code 65) is displayed in Keys.A and so on ...

Hope this helps someone.

+14
source

It should work!

Keys o = (Keys) Enum.Parse (typeof (Keys), ((int) e.KeyChar) .ToString ());

+1
source

My method only works with letters and some other keys.

 string s = char.ToString(e.KeyChar); 

This gives you a string of key equivalents. Something like the output of Keys.ToString() . So, all we need is converting s to its key equivalents!

 (Keys) Enum.Parse(typeof(Keys), char.ToString(e.KeyChar).ToUpper()); 

good luck ...

+1
source

As far as I know, there is no way to match the enumeration of Keys with char . One indicates key codes and modifiers useful for System.Windows.Forms.Control , while the other represents a 16-bit Unicode character (U + 0000 - U + ffff).

For all different concepts.

0
source

In my opinion, this is the ideal solution in terms of readability.

 Keys key = (Keys) Enum.Parse(typeof(Keys), c.ToString(), ignoreCase: true); 
0
source

All Articles