Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to System.Windows.Input.Key

Tags:

c#

I need a function that takes a String as an argument, and returns a System.Windows.Input.Key. E.G:

var x = StringToKey("enter"); // Returns Key.Enter
var y = StringToKey("a"); // Returns Key.A

Is there any way to do this other than if/else's or switch statements?

like image 517
Entity Avatar asked Jun 05 '11 21:06

Entity


1 Answers

Take a look at KeyConverter, it can convert a Key to and from a string.

KeyConverter k = new KeyConverter();
Key mykey = (Key)k.ConvertFromString("Enter");
if (mykey == Key.Enter)
{
    Text = "Enter Key Found";
}
like image 68
BugFinder Avatar answered Sep 24 '22 06:09

BugFinder