{"id":716,"date":"2013-11-29T15:00:57","date_gmt":"2013-11-29T14:00:57","guid":{"rendered":"http:\/\/labalec.fr\/erwan\/?p=716"},"modified":"2013-11-29T17:31:24","modified_gmt":"2013-11-29T16:31:24","slug":"vb-net-and-xna-article-18-get-text-input-from-user","status":"publish","type":"post","link":"https:\/\/labalec.fr\/erwan\/?p=716","title":{"rendered":"VB.Net and XNA : Article 18 &#8211; Get text input from user"},"content":{"rendered":"<p>There are times where you need to get text input from the player like his name, etc.<\/p>\n<p>Although this is possible to do it with XNA only, it is a bit of a hassle : you need to put a timer between each keystroke, etc and result is not perfect.<\/p>\n<p>Lets use <a href=\"http:\/\/nuclexframework.codeplex.com\/\" target=\"_blank\">Nuclex framework<\/a>.<br \/>\nYes the same Nuclex we used in a previous article for gamepad inputs.<\/p>\n<p>Code is rather simpler : we add a reference to nuclex framework, add an event handler and finally draw our string on screen. As simple as that !<\/p>\n<p>Source is there : .<a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/XNA_DEMO_24.zip\">XNA_DEMO_24<\/a><br \/>\nSee vide lower in this article.<\/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    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<br \/>\n    Protected Overrides Sub Initialize()<br \/>\n        MyBase.Initialize()<br \/>\n        AddHandler input.GetKeyboard.CharacterEntered, AddressOf keyboardCharacterEntered<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        MyBase.LoadContent()<br \/>\n    End Sub<br \/>\n    Private Sub keyboardCharacterEntered(character As Char)<br \/>\n        If Asc(character) = 8 Then<br \/>\n            If msg.Length > 0 Then msg = msg.Remove(msg.Length - 1)<br \/>\n        Else<br \/>\n            If Asc(character) >= 32 And Asc(character) <= 126 Then msg += character\n        End If\n    End Sub\n    Protected Overrides Sub UnloadContent()\n        MyBase.UnloadContent()\n    End Sub\n    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)\n        MyBase.Update(gameTime)\n    End Sub\n    Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)\n        GraphicsDevice.Clear(Color.CornflowerBlue)\n        SpriteBatch.Begin()\n        If msg <> \"\" Then SpriteBatch.DrawString(myfont, msg, New Vector2(10, 10), Color.White)<br \/>\n        SpriteBatch.End()<br \/>\n        MyBase.Draw(gameTime)<br \/>\n    End Sub<br \/>\nEnd Class<br \/>\n<\/code><\/p>\n<div style=\"width: 695px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-716-1\" width=\"695\" height=\"417\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/XNA_DEMO_24.mp4?_=1\" \/><a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/XNA_DEMO_24.mp4\">https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/XNA_DEMO_24.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are times where you need to get text input from the player like his name, etc. Although this is possible to do it with XNA only, it is a bit of a hassle : you need to put a timer between each keystroke, etc and result is not perfect. Lets use Nuclex framework. Yes <a href='https:\/\/labalec.fr\/erwan\/?p=716' 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-716","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\/716","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=716"}],"version-history":[{"count":2,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/716\/revisions"}],"predecessor-version":[{"id":720,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/716\/revisions\/720"}],"wp:attachment":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}