문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top