今天教大家怎么在 Unity 開發(fā)的APP中喚起 軟鍵盤坷衍,在surface 上測試OK 溉跃,把踩到的坑都填上了,故而文中代碼開袋即食啸澡。
需求:
在Surface 上點擊了輸入框卸奉,在沒有物理鍵盤的情況下必須彈出軟鍵盤。
實現(xiàn):
- 使用 Process.Start(string path ) 開啟這個軟鍵盤泉蝌。
- 使用 Win32API PostMessage 關(guān)閉軟鍵盤歇万。
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace zFrame.Extend
{
[RequireComponent(typeof(InputField))]
public class TouchKeyBoardComponent : MonoBehaviour, ISelectHandler, IDeselectHandler
{
InputField field;
[SerializeField] bool forcePopup = false;
static Coroutine closeCoroutine = null;
void Start()
{
field = GetComponent<InputField>();
}
static TouchKeyBoardComponent() //靜態(tài)構(gòu)造,無論多少實例全局只執(zhí)行一次勋陪。能夠避免某些時候軟鍵盤首次無法喚起的異常
{
HideKeyboard();
}
#region Coroutine
IEnumerator DelayOpen()
{
yield return new WaitForEndOfFrame(); //延遲開啟 贪磺,避免多個 InputField 輸入數(shù)據(jù)相同的問題
string _file = "C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe";
if (File.Exists(_file))
{
using (Process _process = Process.Start(_file)) { };
}
}
IEnumerator DelayQuit()
{
yield return new WaitForSecondsRealtime(0.1f);
HideKeyboard();
}
#endregion
static void HideKeyboard()
{
try
{
IntPtr _touchhWnd = IntPtr.Zero;
_touchhWnd = FindWindow("IPTip_Main_Window", null);
if (_touchhWnd != IntPtr.Zero) PostMessage(_touchhWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
}
catch { }
}
#region EventSystem Interface Implement
void ISelectHandler.OnSelect(BaseEventData eventData)
{
PointerEventData data = eventData as PointerEventData;
//如果不是鼠標(biāo)左鍵觸發(fā)的選中事件就證明是Touch觸發(fā)的,那么就可以歡快的彈軟鍵盤啦诅愚!
if (forcePopup || (null != data && data.pointerId != -1))
{
if (null != closeCoroutine) //及時的阻止軟鍵盤的關(guān)閉動作寒锚,避免了軟鍵盤的反反復(fù)復(fù)的折疊與展開
{
StopCoroutine(closeCoroutine);
closeCoroutine = null;
}
StartCoroutine("DelayOpen");
}
}
void IDeselectHandler.OnDeselect(BaseEventData eventData)
{
//不關(guān)心哪個輸入框取消了選中都延遲折疊軟鍵盤,因為延遲违孝,所以點了下一個輸入框還能保持軟鍵盤喚起刹前。
if (null == closeCoroutine)
{
closeCoroutine = StartCoroutine("DelayQuit");
}
}
#endregion
#region Win32API Wrapper
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto, EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto, EntryPoint = "PostMessage")]
private static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);
private const Int32 WM_SYSCOMMAND = 274;
private const UInt32 SC_CLOSE = 61536;
#endregion
}
}
填坑:
- 跳選其他輸入框時,軟鍵盤也會反反復(fù)復(fù)折疊又彈出雌桑。
- 解決方案: 將軟鍵盤的關(guān)閉動作寫到協(xié)程中延遲執(zhí)行喇喉,如果在準(zhǔn)備折疊的過程中被喚起,那么就取消折疊校坑,也就避免了這個影響用戶體驗的問題拣技。
- 連續(xù)跳選多個輸入框并彈出軟鍵盤后,只要一輸入撒踪,所有輸入框都會輸入同樣的數(shù)據(jù)过咬。
- 解決方案: 在使用 Process.Start(string path) 開啟軟鍵盤前先延遲一幀即可解決問題。
- 如果一不小心點了哪個屏幕鍵盤的關(guān)閉鍵制妄,然后再怎么點輸入框都無法喚起軟鍵盤怎么辦掸绞?
- 解決方案: 點擊空白區(qū)域停頓大于0.1秒再選中輸入框即可喚起。
- 在接了物理鍵盤時耕捞,怎么都喚不起軟鍵盤衔掸。
- 解決方案: 暫無解決方案,這是Surface 的設(shè)定(應(yīng)該可以找到相關(guān)的設(shè)置項)俺抽。
演示:
- 演示了 1 號坑: 切換輸入框必須先折疊再彈出
-
演示了 2 號坑:多個輸入框輸入相同的數(shù)據(jù)
擴展閱讀:
[Unity 3D] 檢測 Windows 是否接了物理鍵盤 - 簡書
轉(zhuǎn)載請注明出處敞映,謝謝!