forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboardInputField.cs
77 lines (67 loc) · 2.46 KB
/
KeyboardInputField.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace HoloToolkit.UI.Keyboard
{
/// <summary>
/// Class that when placed on an input field will enable keyboard on click
/// </summary>
public class KeyboardInputField : InputField
{
/// <summary>
/// Internal field for overriding keyboard spawn point
/// </summary>
[Header("Keyboard Settings")]
public Transform KeyboardSpawnPoint;
/// <summary>
/// Internal field for overriding keyboard spawn point
/// </summary>
[HideInInspector]
public Keyboard.LayoutType KeyboardLayout = Keyboard.LayoutType.Alpha;
private const float KeyBoardPositionOffset = 0.045f;
/// <summary>
/// Override OnPointerClick to spawn keyboard
/// </summary>
public override void OnPointerClick(PointerEventData eventData)
{
base.OnPointerClick(eventData);
Keyboard.Instance.Close();
Keyboard.Instance.PresentKeyboard(text, KeyboardLayout);
if (KeyboardSpawnPoint != null)
{
Keyboard.Instance.RepositionKeyboard(KeyboardSpawnPoint, null, KeyBoardPositionOffset);
}
else
{
Keyboard.Instance.RepositionKeyboard(transform, null, KeyBoardPositionOffset);
}
// Subscribe to keyboard delegates
Keyboard.Instance.OnTextUpdated += Keyboard_OnTextUpdated;
Keyboard.Instance.OnClosed += Keyboard_OnClosed;
}
/// <summary>
/// Delegate function for getting keyboard input
/// </summary>
/// <param name="newText"></param>
private void Keyboard_OnTextUpdated(string newText)
{
if (!string.IsNullOrEmpty(newText))
{
text = newText;
}
}
/// <summary>
/// Delegate function for getting keyboard input
/// </summary>
/// <param name="sender"></param>
private void Keyboard_OnClosed(object sender, EventArgs e)
{
// Unsubscribe from delegate functions
Keyboard.Instance.OnTextUpdated -= Keyboard_OnTextUpdated;
Keyboard.Instance.OnClosed -= Keyboard_OnClosed;
}
}
}