[Unity] unity 调用触摸键盘

查看:421 |回复:1 | 2021-10-9 10:43:06

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 qq_0.0_Qax 于 2021-10-9 10:46 编辑

clipboard.png
之前一直使用的osk.exe软键盘,总感觉哪里不舒服
之后发现了一个比较好用的触摸键盘Taptip.exe
win7可以直接使用
win10需要打开触摸输入
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;

  6. public class VirtualKeyboard
  7. {
  8.     [DllImport("user32")]
  9.     static extern IntPtr FindWindow(String sClassName, String sAppName);
  10.     [DllImport("user32")]
  11.     static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
  12.     private static Process _onScreenKeyboardProcess = null;
  13.     /// <summary>
  14.     /// Show the touch keyboard (tabtip.exe).
  15.     /// </summary>
  16.     public void ShowTouchKeyboard()
  17.     {
  18.         ExternalCall("C:\\Program Files\\Common Files\\Microsoft Shared\\ink\\tabtip.exe", null, false);
  19.         //ExternalCall("TABTIP", null, false);
  20.     }
  21.     /// <summary>
  22.     /// Hide the touch keyboard (tabtip.exe).
  23.     /// </summary>
  24.     public void HideTouchKeyboard()
  25.     {
  26.         uint WM_SYSCOMMAND = 274;
  27.         int SC_CLOSE = 61536;
  28.         IntPtr ptr = FindWindow("IPTip_Main_Window", null);
  29.         PostMessage(ptr, WM_SYSCOMMAND, SC_CLOSE, 0);
  30.     }
  31.     /// <summary>
  32.     /// Show the on screen keyboard (osk.exe).
  33.     /// </summary>
  34.     public void ShowOnScreenKeyboard()
  35.     {
  36.         //ExternalCall("C:\\Windows\\system32\\osk.exe", null, false);
  37.         if (_onScreenKeyboardProcess == null || _onScreenKeyboardProcess.HasExited)
  38.             _onScreenKeyboardProcess = ExternalCall("OSK", null, false);
  39.     }
  40.     /// <summary>
  41.     /// Hide the on screen keyboard (osk.exe).
  42.     /// </summary>
  43.     public void HideOnScreenKeyboard()
  44.     {
  45.         if (_onScreenKeyboardProcess != null && !_onScreenKeyboardProcess.HasExited)
  46.             _onScreenKeyboardProcess.Kill();
  47.     }
  48.     /// <summary>
  49.     /// Set size and location of the OSK.exe keyboard, via registry changes. Messy, but only known method.
  50.     /// </summary>
  51.     /// <param name='rect'>
  52.     /// Rect.
  53.     /// </param>
  54.     public void RepositionOnScreenKeyboard(Rect rect)
  55.     {
  56.         ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowLeft /t REG_DWORD /d " + (int)rect.x + " /f", true);
  57.         ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowTop /t REG_DWORD /d " + (int)rect.y + " /f", true);
  58.         ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowWidth /t REG_DWORD /d " + (int)rect.width + " /f", true);
  59.         ExternalCall("REG", @"ADD HKCU\Software\Microsoft\Osk /v WindowHeight /t REG_DWORD /d " + (int)rect.height + " /f", true);
  60.     }
  61.     private static Process ExternalCall(string filename, string arguments, bool hideWindow)
  62.     {
  63.         ProcessStartInfo startInfo = new ProcessStartInfo();
  64.         startInfo.FileName = filename;
  65.         startInfo.Arguments = arguments;
  66.         // if just command, we don't want to see the console displayed
  67.         if (hideWindow)
  68.         {
  69.             startInfo.RedirectStandardOutput = true;
  70.             startInfo.RedirectStandardError = true;
  71.             startInfo.UseShellExecute = false;
  72.             startInfo.CreateNoWindow = true;
  73.         }
  74.         Process process = new Process();
  75.         process.StartInfo = startInfo;
  76.         process.Start();
  77.         return process;
  78.     }
  79. }
点击此处复制文本





评分

参与人数 2元素币 +5 活跃度 +8 展开 理由
Adeline + 8 【点赞】这很有大网气质!
qq_LXY_eeb + 5 【感谢】楼主分享的内容!很棒!

查看全部评分

2021-10-9 10:43:06  
 赞 赞 0

使用道具 登录

1个回答,把该问题分享到群,邀请大神一起回答。
2#
谢谢分享
回复 收起回复
2021-10-10 01:19:11   回复
 赞 赞 0

使用道具 登录

CG 游戏行业专业问题

手机游戏引擎手游引擎Unity3D技术
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表