Virtual Keyboard Overview

The virtual keyboard APIs support a rich user experience for text entry.

Use these APIs to display a virtual keyboard and respond to virtual key press events. Examples are shown below.

Note The virtual keyboard can contain a wide variety of characters, not just traditional ASCII. While there are options to limit what keys are present for the onscreen keyboard, there is still the possibility of characters in a wide variety of languages and iconography. In addition, the virtual keyboard can also contain data from SmartGlass and Xbox app devices, including things such as custom characters from unique software keyboards on a wide variety of operating systems. Because there is no guarantee that you will not get unwanted or unsupported characters, you should always filter out characters that your title will not support when using the virtual keyboard.

C++ Sample Code

Here are some code examples.

Show the Keyboard

  1. Get the InputPane object associated with the application window that is currently visible.

    C++

      Windows.UI.ViewManagement.InputPane inputPane = InputPane::GetForCurrentView();  
    
  2. Set the focus to a text box and display the keyboard

    C++

      myTextBox->Focus();
      inputPane->Visible = true;  
    
  3. Get the size of the InputPane object and resize the UI if needed

    C++

      Rect rect = inputPane->OccludedRect;  
    
  4. When finished, do not display the keyboard any longer

    C++

      inputPane->Visible = false;  
    

Handling a Keyboard Event

C++

InputPane inputPane = InputPane::GetForCurrentView();

auto showEventToken = inputPane->Showing += ref new TypedEventHandler<InputPane^, 
  InputPaneVisibilityEventArgs^>([&occludedRect, &showingCount, showEvent] 
    (InputPane^ pane, InputPaneVisibilityEventArgs^ args)

// Optionally, get the occludedRect and manually move the focused element into view 
Rect rect = args->OccludedRect;
              
// ..do the work necessary to move the focused element into view..
             
// Then let the framework know that it doesn't need to move the focused element into view
args->EnsuredFocusedElementInView = true;

// Otherwise, leave args->EnsuredFocusedElementInView false to allow the UX framework to
// move the focused element into view

// Explicitly set visibility to true, or this event could be fired when focus is in an editable control
// and the user presses 'A' on the gamepad
inputPane->Visible = true;  

Getting Text from the Virtual Keyboard

C++

using namespace Windows::Foundation;
using namespace Windows::Xbox::UI

  // Start the async show keyboard operation to retrieve an email address from the user
  auto operation = SystemUI::ShowVirtualKeyboardAsync(L"default text", L"keyboard title",  L"keyboard description", InputScope::EmailSmtpAddress);

  // Hook up our completion handler that will be called when the ShowAsync call is finished
  operation->Completed = ref new AsyncOperationCompletedHandler<Platform::String^>(
                [] (IAsyncOperation<Platform::String^>^ result, AsyncStatus status)
  
  switch (status)
  {
    case AsyncStatus::Completed:
    // HandleConfirmedTextEntry(result->GetResults());
    break;

    case AsyncStatus::Canceled:
    // HandleCanceledTextEntry();
    break;

    case AsyncStatus::Error:
    // HandleErrorTextEntry(result->ErrorCode);
    break;

    default:
    _ASSERT(false);
  }