Unity InputFiled输入框调用win10平板虚拟键盘

传送门→:https://answers.unity.com/questions/1134775/on-screen-keyboard-pc-and-console-best-practices.html

话不都说,直接上代码,复制粘贴到你的项目中

1、VirtualKeyboard.cs

using UnityEngine;
using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class VirtualKeyboard
{
    [DllImport("user32")]
    static extern IntPtr FindWindow(String sClassName, String sAppName);
    [DllImport("user32")]
    static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
    private static Process _onScreenKeyboardProcess = null;
    /// 
    /// Show the touch keyboard (tabtip.exe).
    /// 
    public void ShowTouchKeyboard()
    {
        ExternalCall("C:\\Program Files\\Common Files\\Microsoft Shared\\ink\\tabtip.exe", null, false);
        //ExternalCall("TABTIP", null, false);
    }
    /// 
    /// Hide the touch keyboard (tabtip.exe).
    /// 
    public void HideTouchKeyboard()
    {
        uint WM_SYSCOMMAND = 274;
        int SC_CLOSE = 61536;
        IntPtr ptr = FindWindow("IPTip_Main_Window", null);
        PostMessage(ptr, WM_SYSCOMMAND, SC_CLOSE, 0);
    }
    /// 
    /// Show the on screen keyboard (osk.exe).
    /// 
    public void ShowOnScreenKeyboard()
    {
        //ExternalCall("C:\\Windows\\system32\\osk.exe", null, false);
        if (_onScreenKeyboardProcess == null || _onScreenKeyboardProcess.HasExited)
            _onScreenKeyboardProcess = ExternalCall("OSK", null, false);
    }
    /// 
    /// Hide the on screen keyboard (osk.exe).
    /// 
    public void HideOnScreenKeyboard()
    {
        if (_onScreenKeyboardProcess != null && !_onScreenKeyboardProcess.HasExited)
            _onScreenKeyboardProcess.Kill();
    }
    /// 
    /// Set size and location of the OSK.exe keyboard, via registry changes. Messy, but only known method.
    /// 
    /// 
    /// Rect.
    /// 
    public void RepositionOnScreenKeyboard(Rect rect)
    {
        ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowLeft /t REG_DWORD /d " + (int)rect.x + " /f", true);
        ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowTop /t REG_DWORD /d " + (int)rect.y + " /f", true);
        ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowWidth /t REG_DWORD /d " + (int)rect.width + " /f", true);
        ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowHeight /t REG_DWORD /d " + (int)rect.height + " /f", true);
    }
    private static Process ExternalCall(string filename, string arguments, bool hideWindow)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = filename;
        startInfo.Arguments = arguments;
        // if just command, we don't want to see the console displayed
        if (hideWindow)
        {
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;
        }
        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();
        return process;
    }
}

2、OpenVirtualKeyboard.cs

public class OpenVirtualKeyboard
{
    static VirtualKeyboard vk = new VirtualKeyboard();

    //打开键盘
    public static void OpenKeyBoard()
    {
        vk.ShowTouchKeyboard();
    }
    
    //关闭键盘
    public static void CloseKeyBoard()
    {
        vk.HideTouchKeyboard();
    }

}

3、完成功能:使用InputFiled,点击焦点时弹出,失去焦点时关闭

     你需要将下面脚本挂到InputField上

   InputFiledSelect .cs

using UnityEngine;
using UnityEngine.EventSystems;

public class InputFiledSelect :  MonoBehaviour,ISelectHandler,IDeselectHandler
{
    public void OnDeselect(BaseEventData eventData)
    {
         OpenVirtualKeyboard.CloseKeyBoard();
    }

    public void OnSelect(BaseEventData eventData)
    {
       OpenVirtualKeyboard.OpenKeyBoard();
    }
}

如果运行成功,求个赞赞赞赞赞

你可能感兴趣的:(Unity,C#)