Windows Phone 7 : Reading the Keyboard and Text Input (part 2) - Prompting the User to Enter Text

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

2. Prompting the User to Enter Text

The Keyboard object provides a simple way to read the keyboard for controlling a game, but if you want your user to enter text it is generally not the best approach. The two main reasons are that it takes quite a lot of code to process all the keyboard state changes to build up the user's text string, and (more importantly) that users without keyboards will be completely unable to enter any text at all.

We can prompt the user to enter text using the SIP, which resolves both of the issues with reading the keyboard directly: the code is simple to develop and use, and the onscreen keyboard means that users relying on the touch screen for text entry can still continue to play your game. An example of this input dialog can be found in the SoftInputPanel example project.

To initiate text entry, we use the XNA Guide class and call its static BeginShowKeyboardInput method. This will cause the screen to be taken over by a text input box with the SIP displayed for touch screen users. We can provide a title for the input dialog, a message for the user, and a default value to display within the text input area. A screenshot of the input screen can be seen in Figure 1.

Figure 1. Entering text using the SIP

The code required to initiate the input panel shown in Figure 1 is shown in Listing 3. It first ensures that the keyboard is not already visible and then opens the input window for the user to use.

Example 3. Displaying the text entry dialog window
// Make sure the input dialog is not already visible
if (!(Guide.IsVisible))
// Show the input dialog to get text from the user
Guide.BeginShowKeyboardInput(PlayerIndex.One, "High score achieved",
"Please enter your name", "My name", InputCallback, null)

From left to right, the parameters for BeginShowKeyboardInput are as follows:

  • player. This is the number of the player for whom the dialog is to be displayed. Because we have only single-player support on the phone, this will always be set to PlayerIndex.One.

  • title. The title will be displayed at the top of the input dialog.

  • description. The description will be shown below the title in smaller text.

  • defaultText. An initial value to display in the input field.

  • callback. The address of a function that will be called once the input dialog is complete.

  • state. A user-provided object for the input dialog. This can be passed as null.

When the input dialog completes (by the user entering some text and clicking the OK button, or by clicking the Cancel button), XNA will call into the function specified in the callback parameter. This function must be declared with void return type, and with a single parameter of type IAsyncResult.

When the function is called, it can read the user-entered string by calling the Guide.EndShowKeyboardInput method, passing in the IAsyncResult object. This will return either a string containing the entered string or null if the input dialog was canceled. Listing 4 shows the implementation of the callback function from the SoftInputPanel example.

Example 4. A callback function for the text entry dialog window
void InputCallback(IAsyncResult result)
string sipContent = Guide.EndShowKeyboardInput(result);

// Did we get some input from the user?
if (sipContent != null)
// Store it in the text object
((TextObject)GameObjects[0]).Text = "Your name is " + sipContent;
// The SIP was canceled
((TextObject)GameObjects[0]).Text = "Name entry was canceled.";

One thing to be aware of when using the input dialog is that it is not synchronous. You might expect that your game will stop running while the dialog is open, but this is not the case: the game continues to run in the background the whole time.

There might be some useful aspects to this—for example, it will allow you to keep your music and sound effects generating . In terms of the game, however, you might want to have this pause while the dialog is open.

We can achieve this very easily by checking the Guide.IsVisible property (which you already saw in Listing 3). If this returns true, skip updating the game objects or any other game logic during that call to Update. Once the function returns false, the dialog has closed, and updates can be resumed once again.

  •  Galaxy Note II vs Galaxy Mega
  •  It's Not Finished Yet!
  •  Looking For Advice On Choosing The Best Phone?
  •  Windows Phone 8 : Working with the Camera (part 3) - Camera Lens App
  •  Windows Phone 8 : Working with the Camera (part 2) - Raw Hardware Access
  •  Windows Phone 8 : Working with the Camera (part 1) - Using the PhotoCamera Class
  •  BlackBerry Development : Pushing Data to External Users - Web Signals (part 6) - Building a Web Signal - Unsubscribing from a Subscription
  •  BlackBerry Development : Pushing Data to External Users - Web Signals (part 5) - Building a Web Signal - Requesting the Status of a Subscription
  •  BlackBerry Development : Pushing Data to External Users - Web Signals (part 4) - Building a Web Signal - Pushing Data to Subscribers
  •  BlackBerry Development : Pushing Data to External Users - Web Signals (part 3) - Building a Web Signal - Web Signal Domains, Web Signal Subscriber Registration
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us