Unity 调用系统自带的虚拟键盘

说明

两种方式启动了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;
		}
	}
}

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值