美文网首页unity
[Unity 3d] TouchKeyBoardComponen

[Unity 3d] TouchKeyBoardComponen

作者: 雨落随风 | 来源:发表于2019-09-16 22:55 被阅读0次

今天教大家怎么在 Unity 开发的APP中唤起 软键盘,在surface 上测试OK ,把踩到的坑都填上了,故而文中代码开袋即食。

需求:

在Surface 上点击了输入框,在没有物理键盘的情况下必须弹出软键盘。

实现:

  • 使用 Process.Start(string path ) 开启这个软键盘。
  • 使用 Win32API PostMessage 关闭软键盘。
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()  //静态构造,无论多少实例全局只执行一次。能够避免某些时候软键盘首次无法唤起的异常
        {
            HideKeyboard();
        }

        #region Coroutine 
        IEnumerator DelayOpen()
        {
            yield return new WaitForEndOfFrame();  //延迟开启 ,避免多个 InputField 输入数据相同的问题
            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;
            //如果不是鼠标左键触发的选中事件就证明是Touch触发的,那么就可以欢快的弹软键盘啦!
            if (forcePopup || (null != data && data.pointerId != -1))
            {
                if (null != closeCoroutine) //及时的阻止软键盘的关闭动作,避免了软键盘的反反复复的折叠与展开
                {
                    StopCoroutine(closeCoroutine);
                    closeCoroutine = null;
                }
                StartCoroutine("DelayOpen");
            }
        }

        void IDeselectHandler.OnDeselect(BaseEventData eventData)
        {
            //不关心哪个输入框取消了选中都延迟折叠软键盘,因为延迟,所以点了下一个输入框还能保持软键盘唤起。
            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
    }
}

填坑:

  1. 跳选其他输入框时,软键盘也会反反复复折叠又弹出。
    • 解决方案: 将软键盘的关闭动作写到协程中延迟执行,如果在准备折叠的过程中被唤起,那么就取消折叠,也就避免了这个影响用户体验的问题。
  2. 连续跳选多个输入框并弹出软键盘后,只要一输入,所有输入框都会输入同样的数据。
    • 解决方案: 在使用 Process.Start(string path) 开启软键盘前先延迟一帧即可解决问题。
  3. 如果一不小心点了哪个屏幕键盘的关闭键,然后再怎么点输入框都无法唤起软键盘怎么办?
    • 解决方案: 点击空白区域停顿大于0.1秒再选中输入框即可唤起。
  4. 在接了物理键盘时,怎么都唤不起软键盘。
    • 解决方案: 暂无解决方案,这是Surface 的设定(应该可以找到相关的设置项)。

演示:

  1. 演示了 1 号坑: 切换输入框必须先折叠再弹出
  2. 演示了 2 号坑:多个输入框输入相同的数据


扩展阅读:

[Unity 3D] 检测 Windows 是否接了物理键盘 - 简书

转载请注明出处,谢谢!

相关文章

网友评论

    本文标题:[Unity 3d] TouchKeyBoardComponen

    本文链接:https://www.haomeiwen.com/subject/deafuctx.html