locked
Keyboard unable to responds for my code. RRS feed

  • Question

  • Hi,

    Im new to C# and im working on XNA and im having problems with the aKeyboardState method.

    I tried using the aKeyboardState method for the second time in the second switch-case method but the arrows keys on the keyboard is not responding. 

    while the arrows key is responding in the first switch-case method.

    protected override void Update(GameTime gameTime)
            {
                bg.updateGraphic(Content);
                KeyboardState aKeyboardState = Keyboard.GetState();
    
                //Escape key to exit game
                if (aKeyboardState.IsKeyDown(Keys.Escape) == true)
                {
                    this.Exit();
                }
    
    
                switch (mCurrentScreen)
                {
                    case Screen.MainScreen:
                        {
                            if (aKeyboardState.IsKeyDown(Keys.X) == true) //X key to open the Main Game Menu
                            {
                                mCurrentScreen = Screen.MainMenu;
                            }
                            break;
                        }
    
                    case Screen.MainMenu:
                        {
                            //Move the currently highlighted menu option 
                            //up and down depending on what key the user has pressed
                            if (aKeyboardState.IsKeyDown(Keys.Right) == true && mPreviousKeyboardState.IsKeyDown(Keys.Right) == false)
                            {
                                //Move selection right
                                switch (mMainScreenOption)
                                {
                                    case MainScreenOption.SinglePlayerSelect:
                                        {
                                            System.Diagnostics.Debug.WriteLine("options");
                                            mMainScreenOption = MainScreenOption.OptionSelect;
                                        }
                                        break;
    
                                    case MainScreenOption.OptionSelect:
                                        {
                                            System.Diagnostics.Debug.WriteLine("high score");
                                            mMainScreenOption = MainScreenOption.HighScoreSelect;
    
                                            break;
                                        }
                                    case MainScreenOption.HighScoreSelect:
                                        {
                                            System.Diagnostics.Debug.WriteLine("credit");
                                            mMainScreenOption = MainScreenOption.CreditSelect;
                                            break;
                                        }
    
    
                                }
    
                            }
    
    
    
                            if (aKeyboardState.IsKeyDown(Keys.Left) == true && mPreviousKeyboardState.IsKeyDown(Keys.Left) == false)
                            {
                                //Move selection up
                                switch (mMainScreenOption)
                                {
                                    case MainScreenOption.CreditSelect:
                                        {
                                            System.Diagnostics.Debug.WriteLine("options1");
                                            mMainScreenOption = MainScreenOption.HighScoreSelect;
                                            break;
                                        }
                                    case MainScreenOption.HighScoreSelect:
                                        {
                                            System.Diagnostics.Debug.WriteLine("options2");
                                            mMainScreenOption = MainScreenOption.OptionSelect;
                                            break;
                                        }
                                    case MainScreenOption.OptionSelect:
                                        {
                                            System.Diagnostics.Debug.WriteLine("options3");
                                            mMainScreenOption = MainScreenOption.SinglePlayerSelect;
                                            break;
                                        }
    
                                }
    
    
                                //If the user presses the "X" key, move the state to the 
                                //appropriate game state based on the current selection
    
                            }
    
                            if (aKeyboardState.IsKeyDown(Keys.X) == true) //X key to open the Main Game Menu
                            {
                                switch (mMainScreenOption)
                                {
                                    case MainScreenOption.OptionSelect:
                                        {
                                            System.Diagnostics.Debug.WriteLine("IN options");
    
                                            mMainScreenOption = MainScreenOption.OptionMenu;
                                            break;
                                        }
                                    case MainScreenOption.OptionMenu:
                                        {
                                            if (aKeyboardState.IsKeyDown(Keys.Right) == true && mPreviousKeyboardState.IsKeyDown(Keys.Right) == false)
                                             {
                                                switch(mSelectOption)
                                                {
                                                    case SelectOption.Volume:
                                                        {
                                                            System.Diagnostics.Debug.WriteLine("Volume");
                                                            mSelectOption = SelectOption.Graphic;
                                                            break;
                                                        }
                                                    case SelectOption.Graphic:
                                                        {
                                                            System.Diagnostics.Debug.WriteLine("graphic");
                                                             mSelectOption = SelectOption.Controls;
                                                             break;
                                                         }
                                                }
                                            }
    
                                            if (aKeyboardState.IsKeyDown(Keys.Left) == true && mPreviousKeyboardState.IsKeyDown(Keys.Left) == false)
                                            {
                                                switch (mSelectOption)
                                                {
                                                    case SelectOption.Controls:
                                                        {
                                                            System.Diagnostics.Debug.WriteLine("graphic");
                                                            mSelectOption = SelectOption.Graphic;
                                                            break;
                                                        }
                                                    case SelectOption.Graphic:
                                                        {
                                                            System.Diagnostics.Debug.WriteLine("volume");
                                                            mSelectOption = SelectOption.Volume;
                                                            break;
                                                        }
                                                }
                                            }
    
    
                                            break;
                                        }
                                                
    
    
                                }
                            }
    
                                break;
    
                            }
                        }
                
                mPreviousKeyboardState = aKeyboardState;
                base.Update(gameTime);
            }

    • Moved by Bob Shen Monday, May 7, 2012 2:55 AM (From:Visual C# Language)
    Friday, April 20, 2012 1:15 PM

All replies

  • You'll have better luck getting answers to XNA related questions on the XNA forums.
    Friday, April 20, 2012 1:41 PM
  • Thanks
    Friday, April 20, 2012 1:48 PM