C#开发winform调用软键盘

基于微软提供的osk.exe(On-Screen Keyboard)实现软键盘功能。

窗体上有一个文本框和按钮,点击按钮时打开软键盘同时文本框获取焦点,接收软键盘的输入。

主要代码:

DllImport("kernel32.dll", SetLastError = true)\] private static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr); \[DllImport("kernel32.dll", SetLastError = true)\] private static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr); private void button1_Click(object sender, EventArgs e) { bool isKeyboadRunning = Process.GetProcessesByName("osk").Length \> 0; if (isKeyboadRunning) return; IntPtr ptr = IntPtr.Zero; bool redirected = Wow64DisableWow64FsRedirection(ref ptr); try { Process.Start("osk.exe"); } finally { if (redirected) { Wow64RevertWow64FsRedirection(ptr); } textBox1.Focus(); } } 程序发布后,从网上下载osk.exe和msswch.dll,放到发布后的目录中。 ![](https://i-blog.csdnimg.cn/direct/1f257006ce744a528c8245e7392ac0ee.png) 程序运行后,点击按钮可打开软键盘。 ![](https://i-blog.csdnimg.cn/direct/7bbaea2bd2984bb895c5904b2a8d2b46.png)

相关推荐
谢大旭2 小时前
iframe 三档预览模式
c#
她说彩礼65万3 小时前
C# Sqlite帮助类
jvm·sqlite·c#
q***18063 小时前
如何使用C#与SQL Server数据库进行交互
数据库·c#·交互
炼钢厂3 小时前
WinFrom窗体开发之鼠标交互
windows·microsoft·c#·鼠标
张人玉4 小时前
VsionMaster筛选机错误情况
数码相机·c#·通讯·网络通讯
Crazy Struggle7 小时前
告别人工干预!C# 轻量级上位机自动联动 MES 与视觉检测
c#·.net·mes
LateFrames7 小时前
做【秒开】的程序:WPF / WinForm / WinUI3 / Electron
electron·c#·wpf·winform·winui3·claude code
想拿大厂offer8 小时前
Windows Server 2022 + IIS + .NET 8 + MySQL 部署手册
c#·asp.net
小码编匠10 小时前
.NET 10 性能突破:持续优化才是质变关键
后端·c#·.net