Mobile Keyboard

In most cases, Unity will handle keyboard input automatically for GUI elements but it is also easy to show the keyboard on demand from a script.

iOS

Using the Keyboard

GUI Elements

The keyboard will appear automatically when a user taps on editable GUI elements. Currently, GUI.TextField, GUI.TextArea and GUI.PasswordField will display the keyboard; see the GUI class documentation for further details.

Manual Keyboard Handling

Use the iPhoneKeyboard.Open function to open the keyboard. Please see the iPhoneKeyboard scripting reference for the parameters that this function takes.

Keyboard Type Summary

The Keyboard supports the following types:

iPhoneKeyboardType.DefaultLetters. Can be switched to keyboard with numbers and punctuation.
iPhoneKeyboardType.ASCIICapableLetters. Can be switched to keyboard with numbers and punctuation.
iPhoneKeyboardType.NumbersAndPunctuationNumbers and punctuation. Can be switched to keyboard with letters.
iPhoneKeyboardType.URLLetters with slash and .com buttons. Can be switched to keyboard with numbers and punctuation.
iPhoneKeyboardType.NumberPadOnly numbers from 0 to 9.
iPhoneKeyboardType.PhonePadKeyboard used to enter phone numbers.
iPhoneKeyboardType.NamePhonePadLetters. Can be switched to phone keyboard.
iPhoneKeyboardType.EmailAddressLetters with @ sign. Can be switched to keyboard with numbers and punctuation.

Text Preview

By default, an edit box will be created and placed on top of the keyboard after it appears. This works as preview of the text that user is typing, so the text is always visible for the user. However, you can disable text preview by setting iPhoneKeyboard.hideInput to true. Note that this works only for certain keyboard types and input modes. For example, it will not work for phone keypads and multi-line text input. In such cases, the edit box will always appear. iPhoneKeyboard.hideInput is a global variable and will affect all keyboards.

Visibility and Keyboard Size

There are three keyboard properties in iPhoneKeyboard that determine keyboard visibility status and size on the screen.

visibleReturns true if the keyboard is fully visible on the screen and can be used to enter characters.
areaReturns the position and dimensions of the keyboard.
activeReturns true if the keyboard is activated. This property is not static property. You must have a keyboard instance to use this property.

Note that iPhoneKeyboard.area will return a rect with position and size set to 0 until the keyboard is fully visible on the screen. You should not query this value immediately after iPhoneKeyboard.Open. The sequence of keyboard events is as follows:

  • iPhoneKeyboard.Open is called. iPhoneKeyboard.active returns true. iPhoneKeyboard.visible returns false. iPhoneKeyboard.area returns (0, 0, 0, 0).
  • Keyboard slides out into the screen. All properties remain the same.
  • Keyboard stops sliding. iPhoneKeyboard.active returns true. iPhoneKeyboard.visible returns true. iPhoneKeyboard.area returns real position and size of the keyboard.

Secure Text Input

It is possible to configure the keyboard to hide symbols when typing. This is useful when users are required to enter sensitive information (such as passwords). To manually open keyboard with secure text input enabled, use the following code:

iPhoneKeyboard.Open("", iPhoneKeyboardType.Default, false, false, true);

Hiding text while typing

Alert keyboard

To display the keyboard with a black semi-transparent background instead of the classic opaque, call iPhoneKeyboard.Open as follows:

iPhoneKeyboard.Open("", iPhoneKeyboardType.Default, false, false, true, true);

Classic keyboard

Alert keyboard

Android

Unity Android reuses the iOS API to display system keyboard. Even though Unity Android supports most of the functionality of its iPhone counterpart, there are two aspects which are not supported:

  • iPhoneKeyboard.hideInput
  • iPhoneKeyboard.area

Please also note that the layout of a iPhoneKeyboardType can differ somewhat between devices.

Page last updated: 2013-02-25