調用系統軟件盤支持IL2CPP

using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using TMPro;
namespace zFrame.Extend
{
    [RequireComponent(typeof(TMP_InputField))]
    public class TouchKeyBoardComponent : MonoBehaviour, ISelectHandler, IDeselectHandler
    {
        TMP_InputField field;
        [SerializeField] bool forcePopup = false;
        static Coroutine closeCoroutine = null;

        void Start()
        {
            forcePopup = true;
            field = GetComponent<TMP_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))
            {
                Application.OpenURL(_file);
                field.text = "DelayOpen";
            }
        }
        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
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章