抖音开放平台Logo
开发者文档
控制台

键盘输入法适配

收藏
我的收藏
此文档描述小游戏开发时输入法适配问题。​

自动适配​

在 Unity 小游戏环境的 WebGL 方案中,支持 Unity 2022 及以上版本 Input Field 组件自动适配键盘输入法,无需手动调用。低版本或其他组件暂不支持。​

低版本兼容​

WebGL 方案中 Unity 2021 及以下版本无法正常唤起输入法。我们提供了 StarkKeyboard 模块以及onKeyboardInputEventonKeyboardConfirmEventonKeyboardCompleteEvent等事件,开发者可以通过ShowKeyboardHideKeyboard等方法来显示键盘输入法。​

接口介绍

/// <summary> /// 键盘输入事件 /// <param name="value">键盘输入的当前值</param> /// </summary> public delegate void OnKeyboardInputEvent(string value); /// <summary> /// 用户点击键盘确定按钮时的事件 /// <param name="value">键盘输入的当前值</param> /// </summary> public delegate void OnKeyboardConfirmEvent(string value); /// <summary> /// 键盘收起事件 /// <param name="value">键盘输入的当前值</param> /// </summary> public delegate void OnKeyboardCompleteEvent(string value); /// <summary> /// 监听键盘输入事件 /// </summary> public OnKeyboardInputEvent onKeyboardInputEvent; /// <summary> /// 监听用户点击键盘确定按钮时的事件 /// </summary> public OnKeyboardConfirmEvent onKeyboardConfirmEvent; /// <summary> /// 监听键盘收起事件 /// </summary> public OnKeyboardCompleteEvent onKeyboardCompleteEvent; [StarkVersion(MinSCAndroidVersion = "9999.9999", WebGLMethod = "showKeyboard")] public abstract void ShowKeyboard([CanBeNull] ShowKeyboardOptions options = null, [CanBeNull] Action onSuccess = null, [CanBeNull] Action<string> onError = null); /// <summary> /// 收起软键盘(仅WebGL平台下支持) /// 参考: https://microapp.bytedance.com/docs/zh-CN/mini-game/develop/api/interface/keyboard/tt-hide-keyboard/ /// </summary> /// <param name="onSuccess">隐藏软键盘成功回调: () => {}</param> /// <param name="onError">隐藏软键盘失败回调,(errMsg) => {}</param> [StarkVersion(MinSCAndroidVersion = "9999.9999", WebGLMethod = "hideKeyboard")] public abstract void HideKeyboard([CanBeNull] Action onSuccess = null, [CanBeNull] Action<string> onError = null);

使用示例

using StarkSDKSpace; using UnityEngine; using UnityEngine.UI; public class KeyboardTest : MonoBehaviour { public InputField input; private void Start() { SetInputTexts(); RegisterKeyboardEvents(); } private void SetInputTexts() { input.text = "done"; var comp = input.GetComponent<ClickableInputField>(); if (comp == null) { comp = input.gameObject.AddComponent<ClickableInputField>(); } comp.multiple = false; comp.confirmType = input.text; } private void OnDestroy() { UnregisterKeyboardEvents(); } private void RegisterKeyboardEvents() { StarkSDK.API.GetStarkKeyboard().onKeyboardInputEvent += OnKeyboardInput; StarkSDK.API.GetStarkKeyboard().onKeyboardConfirmEvent += OnKeyboardConfirm; StarkSDK.API.GetStarkKeyboard().onKeyboardCompleteEvent += OnKeyboardComplete; } private void UnregisterKeyboardEvents() { StarkSDK.API.GetStarkKeyboard().onKeyboardInputEvent -= OnKeyboardInput; StarkSDK.API.GetStarkKeyboard().onKeyboardConfirmEvent -= OnKeyboardConfirm; StarkSDK.API.GetStarkKeyboard().onKeyboardCompleteEvent -= OnKeyboardComplete; } private void OnKeyboardInput(string value) { Debug.Log($"OnKeyboardInput: {value}"); if (input.isFocused) { input.text = value; } } private void OnKeyboardConfirm(string value) { Debug.Log($"OnKeyboardConfirm: {value}"); } private void OnKeyboardComplete(string value) { Debug.Log($"OnKeyboardComplete: {value}"); } } public class ClickableInputField : EventTrigger { public string confirmType = "done"; // 可选值有: "done", "next", "search", "go", "send" public int maxInputLength = 100; // 最大输入长度 public bool multiple = false; // 是否多行输入 private InputField _inputField; private void Start() { _inputField = GetComponent<InputField>(); } public override void OnPointerClick(PointerEventData eventData) { if (_inputField != null) { if (_inputField.isFocused) { StarkSDK.API.GetStarkKeyboard().ShowKeyboard(new StarkKeyboard.ShowKeyboardOptions() { maxLength = maxInputLength, multiple = multiple, defaultValue = _inputField.text, confirmType = confirmType }); } } } }