C#调用win10系统自带软键盘的方法
上次做了个笔记是关于调用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 update
void Start()
{
Winvirkey = Process.Start("osk.exe");
Winvirkey.Kill();
}
// Update is called once per frame
void 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