forked from MixedRealityToolkit/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemKeyboardExample.cs
More file actions
171 lines (156 loc) · 5.64 KB
/
Copy pathSystemKeyboardExample.cs
File metadata and controls
171 lines (156 loc) · 5.64 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause
using MixedReality.Toolkit.UX;
using TMPro;
using UnityEngine;
namespace MixedReality.Toolkit.Examples.Demos
{
/// <summary>
/// An example script that delegates keyboard API access either to the WinRT <c>MixedRealityKeyboard</c> class
/// or Unity's <see cref="TouchScreenKeyboard"/> class depending on the platform.
/// </summary>
/// <remarks>
/// <para>Note that like Unity's TouchScreenKeyboard API, this script only supports WSA, iOS, and Android.</para>
/// </remarks>
[AddComponentMenu("MRTK/Examples/System Keyboard Example")]
public class SystemKeyboardExample : MonoBehaviour
{
#if WINDOWS_UWP
private WindowsMRKeyboard wmrKeyboard;
#elif UNITY_IOS || UNITY_ANDROID
private TouchScreenKeyboard touchscreenKeyboard;
#endif
[SerializeField]
private TextMeshPro debugMessage = null;
[SerializeField]
private KeyboardPreview mixedRealityKeyboardPreview = null;
/// <summary>
/// Opens a platform specific keyboard.
/// </summary>
public void OpenSystemKeyboard()
{
#if WINDOWS_UWP
wmrKeyboard.ShowKeyboard(wmrKeyboard.Text, false);
#elif UNITY_IOS || UNITY_ANDROID
if (TouchScreenKeyboard.isSupported)
{
touchscreenKeyboard = TouchScreenKeyboard.Open(string.Empty, TouchScreenKeyboardType.Default, false, false, false, false);
}
#endif
}
#region MonoBehaviour Implementation
/// <summary>
/// A Unity event function that is called on the frame when a script is enabled just before any of the update methods are called the first time.
/// </summary>
private void Start()
{
// Initially hide the preview.
if (mixedRealityKeyboardPreview != null)
{
mixedRealityKeyboardPreview.gameObject.SetActive(false);
}
#if WINDOWS_UWP
// Windows mixed reality keyboard initialization goes here
wmrKeyboard = gameObject.AddComponent<WindowsMRKeyboard>();
if (wmrKeyboard.OnShowKeyboard != null)
{
wmrKeyboard.OnShowKeyboard.AddListener(() =>
{
if (mixedRealityKeyboardPreview != null)
{
mixedRealityKeyboardPreview.gameObject.SetActive(true);
}
});
}
if (wmrKeyboard.OnHideKeyboard != null)
{
wmrKeyboard.OnHideKeyboard.AddListener(() =>
{
if (mixedRealityKeyboardPreview != null)
{
mixedRealityKeyboardPreview.gameObject.SetActive(false);
}
});
}
#elif UNITY_IOS || UNITY_ANDROID
// non-Windows mixed reality keyboard initialization goes here
#else
debugMessage.text = "Keyboard not supported on this platform.";
#endif
}
#if WINDOWS_UWP
/// <summary>
/// A Unity event function that is called every frame, if this object is enabled.
/// </summary>
private void Update()
{
// Windows mixed reality keyboard update goes here
if (wmrKeyboard.Visible)
{
if (debugMessage != null)
{
debugMessage.text = "Typing: " + wmrKeyboard.Text;
}
if (mixedRealityKeyboardPreview != null)
{
mixedRealityKeyboardPreview.Text = wmrKeyboard.Text;
mixedRealityKeyboardPreview.CaretIndex = wmrKeyboard.CaretIndex;
}
}
else
{
var keyboardText = wmrKeyboard.Text;
if (string.IsNullOrEmpty(keyboardText))
{
if (debugMessage != null)
{
debugMessage.text = "Open keyboard to type text.";
}
}
else
{
if (debugMessage != null)
{
debugMessage.text = "Typed: " + keyboardText;
}
}
if (mixedRealityKeyboardPreview != null)
{
mixedRealityKeyboardPreview.Text = string.Empty;
mixedRealityKeyboardPreview.CaretIndex = 0;
}
}
}
#elif UNITY_IOS || UNITY_ANDROID
/// <summary>
/// A Unity event function that is called every frame, if this object is enabled.
/// </summary>
private void Update()
{
// non-Windows mixed reality keyboard initialization goes here
// for non-Windows mixed reality keyboards just use Unity's default
// touch screen keyboard.
if (touchscreenKeyboard != null)
{
string keyboardText = touchscreenKeyboard.text;
if (TouchScreenKeyboard.visible)
{
if (debugMessage != null)
{
debugMessage.text = "typing... " + keyboardText;
}
}
else
{
if (debugMessage != null)
{
debugMessage.text = "typed " + keyboardText;
}
touchscreenKeyboard = null;
}
}
}
#endif
#endregion MonoBehaviour Implementation
}
}