Skip to content

Instantly share code, notes, and snippets.

@SimonDarksideJ
Created June 6, 2013 11:14
Show Gist options
  • Save SimonDarksideJ/5720808 to your computer and use it in GitHub Desktop.
Save SimonDarksideJ/5720808 to your computer and use it in GitHub Desktop.
Sample to close the XNA Guide Keyboard when Phone / System is deactivated (tombstoned)
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace WindowsPhoneGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Maintain a reference for the Guide Result
private IAsyncResult KeyboardResult;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
// Extend battery life under lock.
InactiveSleepTime = TimeSpan.FromSeconds(1);
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void OnDeactivated(object sender, EventArgs args)
{
//Close the guide if present (Must check if it is visible or not first!)
if (Guide.IsVisible && KeyboardResult != null)
{
Guide.EndShowKeyboardInput(KeyboardResult);
}
base.OnDeactivated(sender, args);
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
//Daft example to open the guide keyboard on a single tap
var touchpad = TouchPanel.GetState();
if (touchpad.Count > 0)
{
if (touchpad[0].State == TouchLocationState.Pressed)
{
//Open the guide and show the keyboard
if (!Guide.IsVisible)
{
KeyboardResult = Guide.BeginShowKeyboardInput(PlayerIndex.One, "Test Entry", "A Sample Guide Question", "Say What?", GetTypedChars, null);
}
}
}
base.Update(gameTime);
}
//Guide response handler
protected void GetTypedChars(IAsyncResult r)
{
var typedText = Guide.EndShowKeyboardInput(r);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
@SimonDarksideJ
Copy link
Author

True that the delegate is called when the focus is moved from the messagebox / inputbox, however if you put a breakpoint on the deactivated event you will notice it is fired when the messagebox/inputbox is opened because you app is actually suspended when the guide is opened (like a launcher or chooser it's actually an outside event).
Calling this in deactivated basically clears everything up should the user then take focus away from the app/game with the windows key or holding down the back button

@dancolasanti
Copy link

I see what you mean now - it causes the Guide to be closed sooner - before its UI is displayed. I'll switch to using your idea, but unfortunately, it's still not fixing my specific problem where the Xna hangs on resume from the deactivated game. Looks like it's not just a KeyboardInput issue, MessageBoxes would need to be treated similarly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment