Reputation: 12244
I've got a weird problem and it's most obviously due to my new status as an XNA game programmer but here goes. I've got a KeyboardManager class i made, very simple:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace CrazyCoders.XYZ.Framework
{
public class Keyboard
{
public KeyboardState previousState = Microsoft.Xna.Framework.Input.Keyboard.GetState();
public KeyboardState newState;
public void begin()
{
//Get the new state
KeyboardState newState = Microsoft.Xna.Framework.Input.Keyboard.GetState();
}
public Keys[] detectKeyDowns()
{
return newState.GetPressedKeys().Except(previousState.GetPressedKeys()).ToArray();
}
public Keys[] detectKeyUps()
{
return previousState.GetPressedKeys().Except(newState.GetPressedKeys()).ToArray();
}
public void end()
{
//Save the new state for the next pass
previousState = newState;
}
}
}
This class saves the previous keyboard state and takes the new one when you call begin(). Then using the newState and previousState, the detectKeyDowns and detectKeyUps compute array exceptions to return what was really recently pressed or de-pressed.
Problem is, the thing is not working...
I tried myself to add
if (newState.IsKeyDown(Keys.I))
{
Console.WriteLine(
}
Right after the newState fetch in begin and break on it, it works fine, i see that my "I" key is pressed for instance. But when i call detectKeyDowns, i can seem to except correctly.
Do you guys see anything i'm doing wrong here?
Upvotes: 0
Views: 567
Reputation: 1799
In your begin()
method, you are redefining the newState
variable. Remove "KeyboardState" from the start of your line, and everything should start working.
Upvotes: 1