Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get access to the keyboard state in XNA from an embedded JavaScript runtime.

I'd like to do something similar to the following, but I can't seem to figure out how to get from "Left" to Keys.Left, in one line, for all 100+ keys or so.

js.SetParameter("XNA_Keyboard", new {
    keyDown = (Func<string, bool>)((name) => {
        return keyboardState.IsKeyDown(Keys[name]); // ! Can't index keys enum by name
    }),
});

Is there some kind of reflection that will make this easier?

share|improve this question
up vote 5 down vote accepted
js.SetParameter("XNA_Keyboard", new {
    keyDown = (Func<string, bool>)((name) => {
        return keyboardState.IsKeyDown((Keys)Enum.Parse(typeof(Keys), name, false)); // ! Can't index keys enum by name
    }),
});

That should do the trick, assuming the names match the C# ones.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.