KeyboardState oldState = Keyboard.GetState();
protected override void Update( GameTime gameTime )
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed)
this.Exit();
UpdateInput();
base.Update( gameTime );
}
private void UpdateInput()
{
KeyboardState newState = Keyboard.GetState();
// Check to see whether the Spacebar is down.
if (newState.IsKeyDown( Keys.Space ))
{
// Key has just been pressed.
}
// Otherwise, check to see whether it was down before.
// (and therefore just released)
else if (oldState.IsKeyDown( Keys.Space ))
{
// Key has just been released.
}
oldState = newState;
}
[.net] question using case: for input (XNA)
Hi, im developping my own game atm, and im writing the input part at the moment. I've found code to show me how to implement keyboard input:
Now in my game i want to use the spacebar, the up and down arrow, and the ctrl button.
So it would be stupid to write if statements for them all,
so i want to use the "case:" statement.
The only problem is that i dont know what parameter to give it for each button.
("case newState.IsKeyDown(Keys.Space):" doesn't work).
Im fairly new at this, but if anyone could help me out with this i'd be very thankfull.
Are you saying the player is only allowed to use one key at a time? If not then you wouldn't use a switch block, the if statements are the way to go. Also, your check for a key being pressed is slight incorrect. To check for a key just being pressed you would do:
if (newState.IsKeyDown( Keys.Space ) && !oldState.IsKeyDown( Keys.Space )){ // Key has just been pressed.}
The way I do it is a little different.
I use a map<> of delegates to the functions. It makes it easier to manage then a monolithic switch or if block, at least to me.
theTroll
I use a map<> of delegates to the functions. It makes it easier to manage then a monolithic switch or if block, at least to me.
theTroll
Quote:Original post by TheTroll
The way I do it is a little different.
I use a map<> of delegates to the functions. It makes it easier to manage then a monolithic switch or if block, at least to me.
theTroll
Agreed. For someone just starting out though, understanding the basics is probably a good thing. Also, if the game is only 4 keys it's not a big deal IMO. [grin]
so eehm the idea is that up and down are not pressed simutaniously, but ctrl and space or keyarrow + space might be pressed simultaniously.
should i stick to the if statements?
maybe some1 can make show an example using case's :)
Thanks for the input guys.
should i stick to the if statements?
maybe some1 can make show an example using case's :)
Thanks for the input guys.
Using a switch to check the keyboard means that you'll kick out of the switch when it hits the first check that evaluates to true. So if you're checking for firing a weapon first and moving second you'll never hit the moving check if you're firing the weapon. The same thing done with ifs:
You don't have that problem.
if (newState.IsKeyDown(Keys.Escape) && _oldState.IsKeyDown(Keys.Escape)) { this.Exit(); } if (newState.IsKeyDown(Keys.Space) && _oldState.IsKeyDown(Keys.Space)) { //fire weapon } if (newState.IsKeyDown(Keys.Left) && _oldState.IsKeyDown(Keys.Left)) { //move left }
You don't have that problem.
thanks alot, i totally realise that i cant use case now thanks :)
ill give you a hint when the game is done if you like, thanks for the help again all!
ill give you a hint when the game is done if you like, thanks for the help again all!
Actually you can continue to use case statements.
Call KeyboardState.GetPressedKeys();
which returns an array of Keys[] which you can parse and run through a switch.
I also use this to convert alot of keys (Keys is a large enum) to their ascii version.
string letter = Keys.A.ToString();
Of course you will have to filter out and catch special keys but it beats having to filter out 105+ keys.
Call KeyboardState.GetPressedKeys();
which returns an array of Keys[] which you can parse and run through a switch.
I also use this to convert alot of keys (Keys is a large enum) to their ascii version.
string letter = Keys.A.ToString();
Of course you will have to filter out and catch special keys but it beats having to filter out 105+ keys.
Thanks again, i tryed to use it but i failed :P
anyway, i used the following code to fix it atm:
i realised i used too much code i didn't need :)
anyway, i used the following code to fix it atm:
#region down// Is the down-arrow key down?if (newState.IsKeyDown(Keys.Down)){ fanPosition += moveFactorPerSecond;}else if (oldState.IsKeyDown(Keys.Down)){ // Key was down last update, but not down now, so // it has just been released. }#endregion
i realised i used too much code i didn't need :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement