View Single Post
Old 11-30-2013, 08:01 AM
VB_Alien VB_Alien is offline
Senior Contributor
Join Date: Apr 2004
Posts: 854
Default Xna Keyboard Error

I don't know if this site is really geared towards
the xna framework but i thought that i would ask
anyway. It doesn't hurt to ask, as they always say.

Anyway. I'm using VB Net 2010 pro, along with xna 4.0
and windows phone 7.1.

My trouble is that when i create a new xna game project
and insert the code to detect the keyboard key presses,
i get an error, right from the start up of the application.

Here is the full exception error that i get.

System.InvalidOperationException was unhandled
Message=An invalid operation occurred when trying to query the keyboard state. The result code was 0.
at Microsoft.Xna.Framework.Input.Keyboard.GetState(PlayerIndex playerIndex)
at WindowsGame2.Game1.Update(GameTime gameTime) in C:\Users\xxxxxxxxxxx\Desktop\WindowsGame2\WindowsGame2\WindowsGame2\Ga me1.vb:line 67
at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
at Microsoft.Xna.Framework.Game.Run()
at WindowsGame2.Program.Main(String[] args) in C:\Users\xxxxxxxxx\Desktop\WindowsGame2\WindowsGame2\WindowsGame2\Prog ram.vb:line 9

The first error starts in the game1 class and here it is
with line numbers added.


56    ''' <summary>
57    ''' Allows the game to run logic such as updating the world,
58    ''' checking for collisions, gathering input, and playing audio.
59    ''' </summary>
60    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
61    Protected Overrides Sub Update(ByVal gameTime As GameTime)
62        ' Allows the game to exit
63        If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
64            Me.Exit()
65        End If
67        keyState = Keyboard.GetState() ' <-- Error here
69        If (Keyboard.GetState().IsKeyDown(Keys.Up)) = True Then MsgBox("Up key was pressed")
71        ' TODO: Add your update logic here
72        MyBase.Update(gameTime)
74    End Sub
The second error occurs in the Program.vb class but
this one is understandable since there was already a
previous error.


1   #If WINDOWS Or XBOX Then
3   Module Program
4      ''' <summary>
5      ''' The main entry point for the application.
6      ''' </summary>
7        Sub Main(ByVal args As String())
8          Using game As New Game1()
9              game.Run()  '<--- Error here
10          End Using
11      End Sub
12    End Module
14   #End If
This seems to be a regular problem with xna using both
C# and or VB Net but i have yet, been able to find a
solution that will fix the error.

I was hoping someone here may have had this problem
and found a solution to fix it.
Reply With Quote