MOBILE

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

9/23/2013 9:20:49 PM

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;
}
else
{
// 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.

Other  
  •  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
  •  
    Most View
    Lenovo ThinkPad Twist - The Old Form Factor Starting A New Life With Windows 8 (Part 1)
    Windows 8 : Managing User Access and Security - Managing Remote Access to Workstations (part 2)
    ASRock Z77 Extreme11 Mainboard - An LGA 1155 Mainboard For Users That Use Multiple Drives (Part 5)
    The Review Of Three Seasonic Power Supply Suits (Part 4)
    Windows Phone 7 : Drawing with Vertices and Matrices - Tinting Objects
    Asus PadFone 2 - The Attraction Of The Phone-In-Tablet Combination (Part 6)
    Two Is Better Than One - WD My Cloud Mirror
    Disgo 9104 - An Android Tablet That Runs On Ice Cream Sandwich
    Sharepoint 2010 : Putting Your Site on the Web - Web Content Management (part 1)
    MSI GT70 Dragon Edition Gaming Laptop Review (Part 2)
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
    Top 10
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
    OPEL MERIVA : Making a grand entrance
    FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
    BMW 650i COUPE : Sexy retooling of BMW's 6-series
    BMW 120d; M135i - Finely tuned
    PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
    PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
    Java Tutorials : Nested For Loop (part 2) - Program to create a Two-Dimensional Array
    Java Tutorials : Nested For Loop (part 1)