10 Replies - 2946 Views - Last Post: 06 May 2012 - 09:59 AM

#1 justStartedXNA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-May 12

key pressed or key down? problem with button

Posted 06 May 2012 - 06:13 AM

Hi Folks,
This probably seems like a simple question so apologies for that, but anywho...When the user presses 'exit' a screen loads saying press 'y' to confirm and 'n' to return to game. This however only works if the user is press y or n, while they click the game.

Im using

KeyboardState current_keyboard_state = Keyboard.GetState();

                            if (current_keyboard_state.IsKeyDown(Keys.Y))
                            {
                                ExitGame = true;
                            }

                           if (current_keyboard_state.IsKeyDown(Keys.N))
                            {
                                ExitGame = true;
                            }




iv tried to add previous keyboard state but previous from what, instead of a key down is there something like a key pressed?

Thank You

Is This A Good Question/Topic? 0
  • +

Replies To: key pressed or key down? problem with button

#2 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: key pressed or key down? problem with button

Posted 06 May 2012 - 07:10 AM

You mean you want to detect click event?

I think even in MSDN was such example, something like
currentState = Keyboard.GetState();

//do your stuff
//if in OldState key was press and in currentState is not it means it was recently pushed up, means realeased
//if in OldState key was press up and in currentState is down it means it was recently pushed down, clicked but not realeased
//This will detect clicks only once, because if user keeps key pressed it will be oldState==currentState.

//OldState being in class scope
OldState = Keyboard.GetState();


This post has been edited by Nerfpl: 06 May 2012 - 07:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: key pressed or key down? problem with button

Posted 06 May 2012 - 07:33 AM

To check previous state and current state of keys, you generally create the both states at the top of the class where your other fields are. Then you want to set the previous state to the current state and call the GetState() method for the current state.
KeyboardState currentKBState;
KeyboardState previousKBState;

// In the update code
previousKBState = currentKBState;
currentKBState = Keyboard.GetState();



This will allow you to check if a key was down in the current state, and not down in the previous state, something like:
// New key press. Key is down in the current state, but not down in the previous state
if(currentKBState.IsKeyDown(Keys.Space) && !previousKBState.IsKeyDown(Keys.Space))
{
    // Do something...
}


Was This Post Helpful? 0
  • +
  • -

#4 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: key pressed or key down? problem with button

Posted 06 May 2012 - 07:46 AM

View PostKilorn, on 06 May 2012 - 07:33 AM, said:

previousKBState = currentKBState;
currentKBState = Keyboard.GetState();



Are you sure previousKBState won't return currentKBState by reference?

So when currentKBState = Keyboard.GetState();
previousKBState == currentKBState;

Not sure, just clarifying. I may be wrong

This post has been edited by Nerfpl: 06 May 2012 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

#5 justStartedXNA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-May 12

Re: key pressed or key down? problem with button

Posted 06 May 2012 - 08:20 AM

Thanks for replying.....but

So iv declared this at the top:
KeyboardState currentKBState;
KeyboardState previousKBState;
[code]


And at the beginning of my update I have used:
[code]
previousKBState = currentKBState;
currentKBState = Keyboard.GetState();



//then when the user clicks the exit buton this all happens
if (mouseState.LeftButton == ButtonState.Released && EndGameBTN.Contains(x, y) && (EndGameBtnPressed))
                    {

                        EndGameBtnPressed = false;//so cant b clicked again


                        drawQuit = true;//will draw are you sure you want to quit

                        levelUpdate = false;//stop updates from level

     if(currentKBState.IsKeyDown(Keys.Y) && !previousKBState.IsKeyDown(Keys.Y))
	                            {
                                    quitGame = true;//close the game
	                                
	                            }


So far the ‘are you sure you want to quit’ screen loads fine, but nothing happens depending on the input,

Can anyone see where I am going wrong?

Thankyou
Was This Post Helpful? 0
  • +
  • -

#6 bonyjoe  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 548
  • Joined: 08-September 10

Re: key pressed or key down? problem with button

Posted 06 May 2012 - 08:33 AM

View PostNerfpl, on 06 May 2012 - 03:46 PM, said:

View PostKilorn, on 06 May 2012 - 07:33 AM, said:

previousKBState = currentKBState;
currentKBState = Keyboard.GetState();



Are you sure previousKBState won't return currentKBState by reference?

So when currentKBState = Keyboard.GetState();
previousKBState == currentKBState;

Not sure, just clarifying. I may be wrong


It will not. Though I do prefer your way of setting the previous state at the end of the update instead of the beginning, there seems to be an odd split of people doing it one way or the other though, in my opinion setting it at the end is slightly more readable.

@JustStartedXNA try this

if(currentKBState.IsKeyUp(Keys.Y) && previousKBState.IsKeyDown(Keys.Y))


that way when the key is pushed down and released it should go into the conditional block.

Also where is the boolean quitGame being used? that may be your problem, try putting a break point inside the if statement here and wherever the boolean is used elsewhere, then see if you hit any of them when pressing Y

This post has been edited by bonyjoe: 06 May 2012 - 08:38 AM

Was This Post Helpful? 0
  • +
  • -

#7 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: key pressed or key down? problem with button

Posted 06 May 2012 - 08:41 AM

View PostjustStartedXNA, on 06 May 2012 - 08:20 AM, said:

if(currentKBState.IsKeyDown(Keys.Y) && !previousKBState.IsKeyDown(Keys.Y))
	                            {
                                    quitGame = true;//close the game
	                                
	                            }


So far the ‘are you sure you want to quit’ screen loads fine, but nothing happens depending on the input,


try adding breakpoint at IF to check how conditions are checked and lastly add breakpoint at quitGame to see if it's being set.

Maybe your quitGame doesn't simply work somewhere else in your code.
Was This Post Helpful? 0
  • +
  • -

#8 justStartedXNA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-May 12

Re: key pressed or key down? problem with button

Posted 06 May 2012 - 09:12 AM

so iv changed it to...
 if(currentKBState.IsKeyUp(Keys.Y) && previousKBState.IsKeyDown(Keys.Y))
{
  quitGame = true;
 }



iv set break points up, when the user clicks exit, it runs through this code..
if (mouseState.LeftButton == ButtonState.Released && EndGameBTN.Contains(x, y) && (EndGameBtnPressed))
02	                    {
03	 
04	                        EndGameBtnPressed = false;//so cant b clicked again
05	 
06	 
07	                        drawQuit = true;//will draw are you sure you want to quit
08	 
09	                        levelUpdate = false;//stop updates from level
10	 
11	      if(currentKBState.IsKeyUp(Keys.Y) && previousKBState.IsKeyDown(Keys.Y))
12	                                {
13	                                    quitGame = true;//close the game
14	                                     
15	                                }



when checking the code on breakpoint it by passes this if(currentKBState.IsKeyUp(Keys.Y) && previousKBState.IsKeyDown(Keys.Y)), but they y key has just been pressed?

quitgame works fine, im using it on another click event, so its not the problem,
problem is its not picking up about the 'y' key

please help
thank you
Was This Post Helpful? 0
  • +
  • -

#9 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: key pressed or key down? problem with button

Posted 06 May 2012 - 09:24 AM

This part of code looks ok. 2 bool checks, nothing fancy. The functions are not bugged and they do not have any iffy behaviors.

If you really want us to help you paste full update method. I'm just guessing you assign last and current incorrectly.

Also breakpoint will only actually work with "quitGame = true;". On IF it will prevent you really from pressing Y because VS will pop each time update is run, so my advice there wasn't really best :)
Was This Post Helpful? 0
  • +
  • -

#10 bonyjoe  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 548
  • Joined: 08-September 10

Re: key pressed or key down? problem with button

Posted 06 May 2012 - 09:25 AM

Oh I clearly didn't read your code properly. Well basically, that code shouldn't be in that if statement. You are checking that the endgamebutton has been pressed first, so you can then show the quit screen and ask for confirmation. But you also check for the confirmation inside this if, so you are expecting them to have pressed Y at exactly the same time as quit.

You need a separate conditional that is something like

if(drawQuit)
{
  //is y pressed code etc
}


that is outside of the
if (mouseState.LeftButton == ButtonState.Released && EndGameBTN.Contains(x, y) && (EndGameBtnPressed))

condition

because I'm presuming once the quit screen is shown that condition is never met again thanks to this line
EndGameBtnPressed = false

Was This Post Helpful? 1
  • +
  • -

#11 justStartedXNA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-May 12

Re: key pressed or key down? problem with button

Posted 06 May 2012 - 09:59 AM

lol makes perfect sense now, I was checking it inside the method when it should have been outside.

Anyway its working now, that you so much guys for all the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1