{"id":650,"date":"2013-11-23T13:18:58","date_gmt":"2013-11-23T12:18:58","guid":{"rendered":"http:\/\/labalec.fr\/erwan\/?p=650"},"modified":"2013-11-23T13:18:58","modified_gmt":"2013-11-23T12:18:58","slug":"vb-net-and-xna-article-11-use-a-gamepad","status":"publish","type":"post","link":"https:\/\/labalec.fr\/erwan\/?p=650","title":{"rendered":"VB.Net and XNA : Article 11 &#8211; Use a gamepad"},"content":{"rendered":"<p>One annoying fact around XNA is that it supports natively only xbox gamepads.<\/p>\n<p>Well Nuclex fixes that for us : )<br \/>\nSee here : <a href=\"http:\/\/nuclexframework.codeplex.com\/\" target=\"_blank\">http:\/\/nuclexframework.codeplex.com\/<\/a> .<br \/>\nDownload, unzip and add a reference in your project to nuclex.input.dll.<\/p>\n<p>The source code : <a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/XNA_DEMO_21.zip\">XNA_DEMO_21<\/a> .<\/p>\n<p>Now lets see the code.<\/p>\n<p>All the magic is in the object inputmanager.<br \/>\nThen, just like we would check the state of keyboard keys, we will do the same with the gamepad buttons.<\/p>\n<p>If you want to check the state of the first DirectInput game pad in your system instead, just write:<br \/>\n<code>If (input.GetGamePad(ExtendedPlayerIndex.Five).GetState().Buttons.X = ButtonState.Pressed) Then ...<\/code><br \/>\nNotice that DirectInput Gamepads (as opposed to xbox gamepads) starts at index 5. However, if you have an xbox pad plugged, index 5 will also work.<\/p>\n<p><code><br \/>\nImports Microsoft.Xna.Framework<br \/>\nImports Microsoft.Xna.Framework.Graphics<br \/>\nImports Microsoft.Xna.Framework.Audio<br \/>\nImports Microsoft.Xna.Framework.Content<br \/>\nImports Microsoft.Xna.Framework.Media<br \/>\nImports Microsoft.Xna.Framework.Input<br \/>\nImports Nuclex.Input<\/p>\n<p>Public Class Game<br \/>\n    Inherits Microsoft.Xna.Framework.Game<br \/>\n    Dim graphics As GraphicsDeviceManager<br \/>\n    Dim SpriteBatch As SpriteBatch<br \/>\n    Dim input As InputManager<br \/>\n    Dim myfont As SpriteFont<br \/>\n    Dim msg As String = \".\"<br \/>\n    Dim dummyTexture As Texture2D<br \/>\n    Dim rc As Rectangle<\/p>\n<p>    Public Sub New()<br \/>\n        graphics = New GraphicsDeviceManager(Me)<br \/>\n        input = New InputManager(Services, Window.Handle)<br \/>\n        Components.Add(input)<br \/>\n        Window.Title = \"Test\"<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Initialize()<br \/>\n        MyBase.Initialize()<br \/>\n    End Sub<br \/>\n    Protected Overrides Sub LoadContent()<br \/>\n        SpriteBatch = New SpriteBatch(GraphicsDevice)<br \/>\n        myfont = Content.Load(Of SpriteFont)(\"xnb\\myfont\")<br \/>\n        dummyTexture = New Texture2D(GraphicsDevice, 1, 1)<br \/>\n        '<br \/>\n        Dim c(0) As Color<br \/>\n        c(0) = Color.White<br \/>\n        dummyTexture.SetData(Of Color)(c)<br \/>\n        '<br \/>\n        rc = New Rectangle(100, 100, 10, 10)<br \/>\n        '<br \/>\n        MyBase.LoadContent()<\/p>\n<p>    End Sub<br \/>\n    Protected Overrides Sub UnloadContent()<br \/>\n        MyBase.UnloadContent()<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)<br \/>\n        'right<br \/>\n        If (Keyboard.GetState.IsKeyDown(Keys.Right)) Or (input.GetGamePad(ExtendedPlayerIndex.Five).GetState().Buttons.X = ButtonState.Pressed) Then<br \/>\n            msg = \"right\"<br \/>\n            rc.X += 1<br \/>\n        End If<br \/>\n        'left<br \/>\n        If (Keyboard.GetState.IsKeyDown(Keys.Left)) Or (input.GetGamePad(ExtendedPlayerIndex.Five).GetState().Buttons.A = ButtonState.Pressed) Then<br \/>\n            msg = \"left\"<br \/>\n            rc.X -= 1<br \/>\n        End If<br \/>\n        'up<br \/>\n        If (Keyboard.GetState.IsKeyDown(Keys.Up)) Or (input.GetGamePad(ExtendedPlayerIndex.Five).GetState().Buttons.RightShoulder = ButtonState.Pressed) Then<br \/>\n            msg = \"up\"<br \/>\n            rc.Y -= 1<br \/>\n        End If<br \/>\n        'down<br \/>\n        If (Keyboard.GetState.IsKeyDown(Keys.Down)) Or (input.GetGamePad(ExtendedPlayerIndex.Five).GetState().Buttons.LeftShoulder = ButtonState.Pressed) Then<br \/>\n            msg = \"down\"<br \/>\n            rc.Y += 1<br \/>\n        End If<br \/>\n        MyBase.Update(gameTime)<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)<br \/>\n        GraphicsDevice.Clear(Color.CornflowerBlue)<br \/>\n        SpriteBatch.Begin()<br \/>\n        SpriteBatch.DrawString(myfont, msg, New Vector2(10, 10), Color.White)<br \/>\n        SpriteBatch.Draw(dummyTexture, rc, Color.White)<br \/>\n        SpriteBatch.End()<br \/>\n        MyBase.Draw(gameTime)<br \/>\n    End Sub<\/p>\n<p>End Class<br \/>\n<\/code><\/p>\n<div style=\"width: 695px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-650-1\" width=\"695\" height=\"417\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/XNA11.mp4?_=1\" \/><a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/XNA11.mp4\">https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/XNA11.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>One annoying fact around XNA is that it supports natively only xbox gamepads. Well Nuclex fixes that for us : ) See here : http:\/\/nuclexframework.codeplex.com\/ . Download, unzip and add a reference in your project to nuclex.input.dll. The source code : XNA_DEMO_21 . Now lets see the code. All the magic is in the object <a href='https:\/\/labalec.fr\/erwan\/?p=650' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,35],"tags":[],"class_list":["post-650","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-xna","category-34-id","category-35-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=650"}],"version-history":[{"count":1,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/650\/revisions"}],"predecessor-version":[{"id":653,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/650\/revisions\/653"}],"wp:attachment":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}