Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

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 have been thinking about this issue for a while now and my recent efforts to find an already existing answer have not been successful.

I am trying to implement a menu system via which the player can map custom keys to each of the controls in the game (of which there are not a whole lot of). What i currently have is a functional menu where the user is able to specify which key they want each control bound to. This screen then writes these keys to a simple XML file from which the rest of the game is able to access these key bounds.

My problem is this: The user is able to use both the mouse and the keyboard to bind the controls to, however these are separate types according to XNA. When i go to retrieve the keybinding that has been loaded from the file i dont know which type it is, as it could be either. Is there some way of dealing with multiple types in this case or am i going to have to create my own type which encompasses both types within it?

At this point I am open to any ideas, Thanks in advance.

share|improve this question
up vote 2 down vote accepted

forgive me if i understood your question incorrectly, i have given this some thought in the past and decided that i would create an Enum for each action, and map each action to an enum for the controller method and an enum for the button /key press

this would in theory allow you to use both controller and keyboard (and mouse for that matter at the same time)

all you need to do is code the listening method to return the correct enum set when asking for the mapping of an action

finally when in you update loop that key is pressed you need to fire that event and have any component that is listening for it respond accordingly.

personally i would just create a wrapper class to do this for you and have the components talk to the wrapper (therefore maintaining separation of concerns a bit more)

share|improve this answer

A clarification question: "can your shortcuts consist of both a key AND a mousepress?" e.g. "CTRL" + right-click mouse? In that case you could make a small struct with both a ButtonState and a Keys membervariable to store both. This makes for easy XML serialiation/deserialization.

If you only want EITHER key OR mouse, you could still store the shortcut in a struct like that in the XML file and then after deserializing the XML, you can then iterate through all the shortcut objects and construct a list of Keys-shortcuts and a list of ButtonState-shortcuts based on which membervariable has a value in each shortcut object.

share|improve this answer

Your Answer


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.