首先是創造一個C#的Script,裡面內容直接如下,可以直接複製貼上:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.Diagnostics; using System.Runtime.InteropServices; public class VirtualKeyboardController : MonoBehaviour { void Start () { } void Update () { } 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(); } /// <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 do not 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; } } }然後在其他地方來做呼叫或關閉,當然虛擬鍵盤呼叫出來後,也可以直接按鍵盤右上角的X來關閉:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class XXX : MonoBehaviour { void Start () { VirtualKeyboardController.VirtualKeyboard keyboard = new VirtualKeyboardController.VirtualKeyboard(); keyboard.ShowOnScreenKeyboard(); keyboard.HideOnScreenKeyboard(); } void Update () { } }這樣就可以呼叫和關閉螢幕虛擬鍵盤了,網路上有人說這招在Win 10後就沒辦法叫了,因為Microsoft不再放出虛擬鍵盤的控制權,我自己在Win 10使用是無礙啦......
沒有留言:
張貼留言