上次做了個筆記是關于調用windows系統自帶的觸摸鍵盤的方法:C#調用Windows系統自帶觸摸鍵盤的方法_c# 虛擬鍵盤-CSDN博客
除了調用觸摸鍵盤,我們也可以通過調用win10的自帶軟鍵盤作為輸入途徑。
方法很簡單。
1、添加using System.Diagnostics引用。
2、創建進程Process Winvirkey = Process.Start("osk.exe");
3、打開鍵盤:Winvirkey = Process.Start("osk.exe");
4、關閉鍵盤:Winvirkey.Kill();
具體實現如下:
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;public class Win10key : MonoBehaviour
{Process Winvirkey;// Start is called before the first frame updatevoid Start(){Winvirkey = Process.Start("osk.exe");Winvirkey.Kill();}// Update is called once per framevoid Update(){if(Input.GetKeyDown(KeyCode.F1)){ShowKey();}else if(Input.GetKeyDown(KeyCode.F2)){HideKey();}}//打開虛擬鍵盤void ShowKey(){//此處需檢測Winvirkey進程是否已關閉,否則打開狀態再執行會報錯if (Winvirkey.HasExited){Winvirkey = Process.Start("osk.exe");}}//關閉虛擬鍵盤void HideKey(){//此處需檢測Winvirkey進程是否已打開,否則關閉狀態再執行會報錯if (!Winvirkey.HasExited){Winvirkey.Kill();}}
}
效果如下:C#調用軟鍵盤_嗶哩嗶哩_bilibili