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)

相关推荐
心疼你的一切5 小时前
三菱FX5U PLC与C#通信开发指南
开发语言·单片机·c#
czhc11400756635 小时前
C# 1221
java·servlet·c#
yong99906 小时前
C#实现OPC客户端与S7-1200 PLC的通信
开发语言·网络·算法·c#
先生沉默先7 小时前
c#Socket学习,使用Socket创建一个在线聊天,服务端功能实现,(3)
服务器·学习·c#
superman超哥8 小时前
仓颉热点代码识别深度解析
开发语言·后端·python·c#·仓颉
Lv11770088 小时前
Visual Studio中的接口
ide·笔记·c#·visual studio
阿蒙Amon9 小时前
C#每日面试题-类和结构的区别
开发语言·c#
MyBFuture10 小时前
索引器实战:对象数组访问技巧及命名空间以及项目文件规范
开发语言·前端·c#·visual studio
jghhh0111 小时前
基于C#的串口电子秤测试程序
开发语言·c#
向宇it11 小时前
【unity游戏开发——网络】使用Unity+PurrNet+Heathens+Steam,在 Unity 中通过 Steam与你的朋友建立联系
网络·游戏·unity·c#·游戏引擎·steam