Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

1 Answer 1

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.