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

本文提供了一种在Unity中控制虚拟键盘的方法,包括显示和隐藏触摸键盘及屏幕键盘,通过代码实现对输入字段(InputFiled)的焦点响应,使键盘在获得焦点时弹出,失去焦点时关闭。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

进口代码,生产厂家→:On Screen Keyboard (PC and Console) best practices - Unity Answers

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

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;
    /// <summary>
    /// Show the touch keyboard (tabtip.exe).
    /// </summary>
    public void ShowTouchKeyboard()
    {
        ExternalCall("C:\\Program Files\\Common Files\\Microsoft Shared\\ink\\tabtip.exe", null, false);
        //ExternalCall("TABTIP", null, false);
    }
    /// <summary>
    /// Hide the touch keyboard (tabtip.exe).
    /// </summary>
    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);
    }
    /// <summary>
    /// Show the on screen keyboard (osk.exe).
    /// </summary>
    public void ShowOnScreenKeyboard()
    {
        //ExternalCall("C:\\Windows\\system32\\osk.exe", null, false);
        if (_onScreenKeyboardProcess == null || _onScreenKeyboardProcess.HasExited)
            _onScreenKeyboardProcess = ExternalCall("OSK", null, false);
    }
    /// <summary>
    /// Hide the on screen keyboard (osk.exe).
    /// </summary>
    public void HideOnScreenKeyboard()
    {
        if (_onScreenKeyboardProcess != null && !_onScreenKeyboardProcess.HasExited)
            _onScreenKeyboardProcess.Kill();
    }
    /// <summary>
    /// Set size and location of the OSK.exe keyboard, via registry changes. Messy, but only known method.
    /// </summary>
    /// <param name='rect'>
    /// Rect.
    /// </param>
    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,点击焦点时弹出,失去焦点时关闭

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

   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();
    }
}

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

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春天花花幼稚园的程序员教孩子如何玩键盘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值