How do you convert a character to a key value in XNA
I'开发者_开发百科m trying to convert a character that I have specified in my application configuration file to a XNA keyboard key. How would I parse my character value to a key?
While harryovers has given you an exact answer to your question, perhaps a better solution for configuration files is to convert from a string instead of from a character. That way your configuration file may specify any key by name, not just alpha-numeric ones.
You could use Enum.Parse
to convert the string to an enumeration (MSDN, example).
this should work:
char c = 'a';
Keys cAsKey = (Keys)((int)(char.ToUpper(c)));
bool compareKeys = (cAsKey == Keys.A); //true
If you're targeting Windows, take a look at the KeysConverter class in System.Windows.Forms. Technically XNA Keys != Windows Forms Keys, but internally they use the same integer values.
精彩评论