fraXis
fraXis

Reputation: 3221

How do I assign a string value to System.Windows.Forms.Keys?

I need to set System.Windows.Forms.Keys to a string that I have assigned.

I am using a 3rd party .Net control that lets me assign the HotKey to the control, and it uses System.Windows.Forms.Keys to set the HotKey. For example:

this.systemHotKey1.SetHotKey(System.Windows.Forms.Keys.S); //Assign S as the HotKey

However, System.Windows.Forms.Keys will not let me assign a string to it, I need to assign an actual value to it. For example this works fine:

System.Windows.Forms.Keys.S (for the hotkey S on the keyboard).



But I want to do something like this:

{
string tmpString = "S";
this.systemHotKey1.SetHotKey(System.Windows.Forms.Keys.tmpString);  //This does not work
}



Can someone please show me a way I can assign a string to System.Windows.Forms.Keys so I can accomplish this?

Upvotes: 0

Views: 3750

Answers (3)

Vishal Suthar
Vishal Suthar

Reputation: 17194

Hope, this will do the work:

KeysConverter kc = new KeysConverter();

string tmpString = "S";
Keys key = (Keys)kc.ConvertFromString(tmpString);
this.systemHotKey1.SetHotKey(key);

Upvotes: 0

Jonas W
Jonas W

Reputation: 3260

I think you are after the KeyConverter in namespace System.Windows.Input

KeyConverter k = new KeyConverter(); 
Keys mykey = (Keys)k.ConvertFromString("Enter"); 
if (mykey == Keys.Enter) 
{ 
    Text = "Enter Key Found"; 
} 

Since Keys is an enum you can also parse it like any enum.

string str = /* name of the key */; 
Keys key; 
if(Enum.TryParse(str, true, out key)) 
{ 
    // use key 
} 
else 
{ 
    // str is not a valid key 
} 

Upvotes: 3

davidcoder
davidcoder

Reputation: 938

You can do this by using KeysConverter

string tmpString = "S";
KeysConverter kc = new KeysConverter();
this.systemHotKey1.SetHotKey(kc.ConvertFromString(tmpstring));

Upvotes: 2

Related Questions