This project is read-only.

The keyboard input problem

Aug 10, 2010 at 4:21 AM

   I just create a default XNA Game, and put Xnaviewer into a winform to show the game. 

   private void Form1_Load(object sender, EventArgs e)

        {

            this.xViewer1.RunGame(new Game1());

            this.xViewer1.Focus();

        }

    And in the  game1.cs I just modify the update function:

    protected override void Update(GameTime gameTime)

        {

            mKeyboardInput = Keyboard.GetState();

 

            if (mKeyboardInput.IsKeyDown(Keys.R))

            {

                System.Windows.Forms.MessageBox.Show("R!");

            }

 

            base.Update(gameTime);

        }

 

      When the game is running, I cannot get the keyboard input. 

      Who can help me about this problem?

Oct 16, 2010 at 3:58 AM

Man, create an Event, and handle that to your winforms, like this:
In your Game, add this:

public delegate void KeyboardHandler(KeyboardState keyState);
public event KeyboardHandler Keyboard_States;
protected override void Update(GameTime gameTime)
{
Keyboard_States(Keyboard.GetState());
}
And, in your winforms, add handler to your game KeyboardHandler.
Oct 29, 2010 at 9:46 AM
Joao_Dot_Neto wrote:

Man, create an Event, and handle that to your winforms, like this:
In your Game, add this:

public delegate void KeyboardHandler(KeyboardState keyState);
public event KeyboardHandler Keyboard_States;
protected override void Update(GameTime gameTime)
{
Keyboard_States(Keyboard.GetState());
}
And, in your winforms, add handler to your game KeyboardHandler.

Thanks a lot!

But I still don't know how to "add handler to game KeyboardHandler in winforms", could you please give me a little bit more help about this?

 

Dec 11, 2010 at 5:22 PM

I know this is a very late reply, but in my experience Focus() does nothing...

if you just click on the viewer, it should begin accepting keyboard input.