Question

I'm trying to implement in my game project in C# a way for the user to change the keys made for moving the character (Up key, Left key, and Right key). I created buttons in my option menu, but I'm still trying to figure out what to put in the event when I click on these buttons. I already looked up in the forum, and didn't find anything really helpful and easy to understand for me ...

I want when I click on the button for defining for example the Up button to wait for the next input the player will make, but I didn't find something yet, something simple.

Can someone please help me ? I hope my question is understandable.

Was it helpful?

Solution

Just like Sayse said, I would use The KeyboardState.GetPressedKeys() method and they will return the keys pressed. I think you can check to see if this is null (untested);

while(currentKeyboardState.GetPressedKeys() == null)

Something along those lines you could create a screen (like in battlefield, or CoD) that says "Press key to assign to Forward". Once the user presses a key, you could store the enum. Write the enum to the registry or something like that.

OTHER TIPS

This is completely untested but it seems like it should work in my mind

KeyboardState has a method called GetPressedKeys, so all you should need to do is check if your defined keys are contained within this list.

For example, (untested)

Key UserDefinedUpKey = Keys.Up;

//UpdateMethod...
var keys = currentKeyboardState.GetPressedKeys();
if(keys.Contains(UserDefinedUpKey))
//Up pressed
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top