How to slow down or stop key presses in XNA

后端 未结 12 2166
北恋
北恋 2021-02-01 05:38

I\'ve begun writing a game using XNA Framework and have hit some simple problem I do not know how to solve correctly.

I\'m displaying a menu using Texture2D and using th

12条回答
  •  庸人自扰
    2021-02-01 06:15

    If your application is for a Windows machine, I've had great success using this event driven class that I found here: gamedev.net forum post

    It handles typical key presses and the short pauses before the repeating starts, just like normal text entry in a Windows application. Also included is mouse move/wheel events.

    You can subscribe to the events, for example, using the following code:

    InputSystem.KeyDown += new KeyEventHandler(KeyDownFunction);
    InputSystem.KeyUp += new KeyEventHandler(KeyUpFunction);
    

    Then in the methods themselves:

    void KeyDownFunction(object sender, KeyEventArgs e)
    {
       if(e.KeyCode == Keys.F)
          facepalm();
    }
    
    void KeyUpFunction(object sender, KeyEventArgs e)
    {
       if(e.KeyCode == Keys.F)
          release();
    }
    

    ...and so forth. It really is a great class. I've found it's flexibility much improved over XNA's default keyboard handling. Good Luck!

提交回复
热议问题