说明
两种方式启动了Win10自带的两种虚拟键盘。
tabtip.exe 在有些系统上启动不了(不知道为啥,所以才有了第二种)
代码
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;
public class TouchKeyboard
{
static Process sf;
[DllImport("user32")]
static extern IntPtr FindWindow(String sClassName, String sAppName);
[DllImport("user32")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
static string path = "C:\\Program Files\\Common Files\\Microsoft Shared\\ink\\tabtip.exe";
/// <summary>
/// 显示屏幕键盘
/// </summary>
public static void ShowTouchKeyboard()
{
try
{
ExternalCall(path, null, false);
}
catch (Exception e)
{
UnityEngine.Debug.Log(e);
}
}
/// <summary>
/// 隐藏屏幕键盘
/// </summary>
public static void HideTouchKeyboard()
{
try
{
uint WM_SYSCOMMAND = 274;
int SC_CLOSE = 61536;
IntPtr ptr = FindWindow("IPTip_Main_Window", null);
PostMessage(ptr, WM_SYSCOMMAND, SC_CLOSE, 0);
}
catch (Exception e)
{
UnityEngine.Debug.Log(e);
}
}
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;
}
public static void Open()
{
sf = System.Diagnostics.Process.Start(@"C:\Windows\System32\osk.exe");
}
public static void Close()
{
if(sf != null)
{
sf.Kill();
sf = null;
}
}
}