Skip to main content

TouchScreenKeyboard

A class in Root.Township, the global namespace.

The in-world virtual keyboard players type on with their fingers. Client only; one shared instance, moved into place with Get for whichever text field (IUseVirtualKeyboard) is using it.

Constructors​

MemberWhat it is
TouchScreenKeyboard()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

MemberWhat it is
bool setupSecretKeyIf set, an extra secret toggle key is added to the last row.
27 private and internal fields
MemberWhat it is
private KeyboardSet androidMultiKeyboardSetupAndroid keyboard set. Not used in this build, see MultiKeyboardSetup.
private VirtualKeyboardSetup androidSetupAndroid single layout. Not read in this build.
private AutoComplete autoCompleteThe autocomplete helper, made by ApplyNewAutoCompletionSource.
private float backspaceDelayStartBacking field for BackspaceDelayStart. Default 1.
private float backspaceSingleLetterDurationBacking field for BackspaceSingleLetterDuration. Default 3.
private float continuousBackspaceSpeedBacking field for ContinuousBackspaceSpeed. Default 0.2.
private int currentKeyboardIndexIndex of the layout in use within the keyboard set.
private Vector2 dimensionsOffsetCentre offset of the touch area, recomputed by ResetDimensions.
private float doubleShiftTimerSeconds within which a second shift press locks shift. Default 0.5.
private const string emptyEmpty string constant.
private FunctionKeyDefinition[] functionKeysKeys with their own actions; mapped into MappedFunctionKeys in Awake.
private bool hasDoneAutoCompleteSetupTrue once AutoCompleteSetup has run.
private readonly Dictionary<int, KeyCode> indexToKeyCodeMaps suggestion slot 0 to 3 to key codes F5 to F8.
private string inputBacking field for Input.
private readonly List<ITouchScreenButton> instantiatedButtonsBacking list for InstantiatedButtons, the spawned keys.
private bool isShiftLockedTrue while shift is locked by a double press.
private TouchScreenKeyboard.KeyBoardEdgesInfo keyboardEdgesInfoRow edge keys and spacing for the current layout, used to place extra keys.
private TouchScreenKeyboardKey keyPrefabPrefab for every key.
private static readonly Logger loggerNLog logger for this class.
private Vector2 originOffsetOffset added to every key position.
private TouchScreenKeyboard.OutputEvent outputInspector event raised with the input whenever it changes.
private KeyboardSet pcMultiKeyboardSetupThe keyboard set MultiKeyboardSetup returns.
private float scaleScale from layout positions to local positions.
private VirtualKeyboardSetup setupSingle layout used when multi-layout mode is off.
private Renderer shiftLockMarkerRenderer shown while shift is locked.
private float timeToDoubleShiftReal time until which a second shift press locks shift.
private bool useMultiKeyboardSetupWhen true the keyboard uses a keyboard set with switchable layouts instead of one layout.

Properties​

MemberWhat it is
float BackspaceDelayStart { get; }Seconds a held backspace waits before repeating.
float BackspaceSingleLetterDuration { get; }Seconds a held backspace deletes letter by letter before speeding up.
float ContinuousBackspaceSpeed { get; }Repeat speed for a held backspace.
IUseVirtualKeyboard CurrentUser { get; private set; }The text field currently typing on the keyboard, or null.
string Input { get; set; }The typed text. Setting it trims to the user's character limit and raises InputChanged.
static TouchScreenKeyboard Instance { get; private set; }The keyboard in the scene.
override List<ITouchScreenButton> InstantiatedButtons { get; }Key buttons spawned for the current layout.
bool IsShiftPressed { get; private set; }True while shift is on. Turns off after one character unless shift is locked.
bool IsShowingSecret { get; }Never set, so always false.
Dictionary<KeyCode, FunctionKeyDefinition> MappedFunctionKeys { get; }Function keys with their actions, by key code.
protected override Vector2 Offset { get; }Centre offset of the keyboard's touch area.
2 private and internal properties
MemberWhat it is
private VirtualKeyboardSetup KeyboardSetup { get; }The single layout used when useMultiKeyboardSetup is off (the setup field).
private KeyboardSet MultiKeyboardSetup { get; }The keyboard set used for switching layouts. Always the PC set in this build; the Android set is never read.

Events​

MemberWhat it is
event Action BackPressedOnEmptyRaised when backspace is pressed with no text.
event Action<string> InputChangedRaised with the new text whenever the input changes.
event Action NextPressedRaised when return is pressed.
event Action<bool> ShiftChangedRaised when shift turns on or off.
event Action ToggleSecretDisplayRaised by ToggleSecret, for fields that hide their text.

Methods​

MemberWhat it is
TouchScreenKeyboardKey AddExtraKey(VirtualKeyInfo keyInfo, bool leftSide, int insertRow)Adds a key at the left or right end of a keyboard row.
void ApplyNewAutoCompletionSource(IAutoCompletionSource autoCompletionSource)Sets where autocomplete suggestions come from, adding the suggestion keys the first time.
void ClearFocus()Removes all listeners from its events and tells the current user it lost focus.
void EventSetUpNewKeyboard(int newKeyboardIndex)Switches to another layout of the keyboard set (such as symbols), keeping keys that match.
static TouchScreenKeyboard Get(Vector3 position, Quaternion rotation, IUseVirtualKeyboard newUser)Moves the keyboard to a position and rotation, shows it, clears the input and gives it to a new user.
void Hide(IUseVirtualKeyboard user)Hides the keyboard if the given user is the current one.
void KeyPressed(KeyCode key)Handles a key: types a character, backspace, return, shift, a function key or an autocomplete pick.
void Next()Asks the current user to move to the next field.
void Previous()Asks the current user to move to the previous field.
void SetupNewUser(IUseVirtualKeyboard newUser)Gives the keyboard to a text field and hooks its input and submit handlers.
void ToggleSecret()Raises ToggleSecretDisplay.
void TypeCustom(string custom)Adds a string to the input.
void WordBackspace()Deletes the last word.
protected override void OnDrawGizmos()Draws the key layout in the editor.
protected override void PostTouchCalculation()After touch checks, lets each enabled key settle its press state.
protected override void PreTouchCalculation()Before touch checks, clears each enabled key's finger distance.
protected override void UpdateTouchWithButtons(TouchScreenMenuBase.TouchInput touch)Updates each key's distance to a nearby fingertip.
23 private and internal methods
MemberWhat it is
private void AddAutoCompleteKeys()Spawns the suggestion keys from the keyboard set's completion layout.
private void ApplyCompletion(string completion)Replaces the whole input with a chosen suggestion.
private void AutoCompleteSetup()Once only, when there's an autocomplete source: adds suggestion keys and refreshes them whenever the input changes.
private void Awake()Claims Instance (logs an error for a second keyboard), maps function keys, sets up for the local player and hides itself if unused.
private void BaseKeyboardSetup(VirtualKeyboardSetup baseSetup, int lastEqualIndex)Spawns the keys of a layout after the ones kept from the previous layout, and records row edges and the space key.
private Vector3 CalculateTransitionKeyPosition_L_R(Vector2 edgeKeyPos, float spacing, int stackMultiplier, float spaceOffset)Works out where an extra key goes at the left or right end of a row.
private static TouchScreenKeyboard Get(Vector3 position, Quaternion rotation)Moves the keyboard to a position and rotation, shows it, clears the input and gives it to a new user.
private void GetMinMax(out Vector2 min, out Vector2 max)Finds the bounds of the current layout's key positions.
private void IncludeLetter(string letter)Adds a letter to the input, upper case while shift is on.
private void IncludeNumber(int number)Adds a digit to the input.
private void LocalKeyboardSetup()Sets up the keyboard for the local player and its autocomplete.
private void OnDestroy()Unhooks from PlayerController.LocalPlayerSet.
private void OnValidate()Editor: recomputes the keyboard's dimensions when settings change.
private void PopulateAutoCompletesWithCurrentInput(string currentInput)Fills up to four suggestion keys with completions for the current input and hides the rest.
private void PressShift()Toggles shift. A second press within doubleShiftTimer locks it; another press unlocks.
private int RemoveOnlyNecessaryKeys(int incomingIndex)When switching layout, keeps leading keys both layouts share and destroys the rest. Returns the last shared index.
private void ResetDimensions()Recomputes the touch area size and offset from the layout's key bounds.
private Vector3 ScalePosition(Vector2 original)Turns a layout key position into a local position, using scale and originOffset.
private void SetActive(bool isActive)Shows or hides the keyboard GameObject.
private void SetUpNewKeyboard(int keyboardIndex, int lastEqualIndex)Builds a layout from the keyboard set, adding the secret-toggle key when setupSecretKey is on.
private void SetupPlayer(PlayerController player)Handler for the local player being set: runs LocalKeyboardSetup once.
private TouchScreenKeyboardKey SpawnKey(VirtualKeyInfo keyInfo)Instantiates a key from keyPrefab under the buttons parent.
private string WordBackspace(string input)Deletes the last word.

Used in​

Members of other types that take, return or hold a TouchScreenKeyboard: