Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting a string to Keys in C#

Lets say we store KeyCode value as a string. How do you convert it back to KeyCode?

For example, I've captured a key on keydown event:

string modifier = e.Modifiers.ToString(); // Control
string key_string = e.KeyCode.ToString(); // D1

How to do the following ?

Keys old_key_restored = (Keys)key_string;

Code above doesn't work.

EDIT: Daniel is a life savior ;)

Keys key_restored = (Keys) Enum.Parse(typeof(Keys), key_key);
like image 848
Alex Avatar asked Dec 02 '25 09:12

Alex


1 Answers

Its just an enum so you can use Enum.TryParse

like image 170
Daniel A. White Avatar answered Dec 04 '25 01:12

Daniel A. White



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!