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,放到发布后的目录中。

程序运行后,点击按钮可打开软键盘。

相关推荐
玩泥巴的2 小时前
MAF + 飞书:让大模型干活,但别让它乱干
ai·c#·.net·飞书·agent
cpp_w6 小时前
C#基础3- 面向对象中级应用3
后端·c#
步步为营DotNet7 小时前
Async 状态机要搬进运行时了:.NET 10 Runtime Async 实测,快的地方快 1.5 倍,贵的地方贵 5 倍
性能优化·c#·.net·async·runtime
hixiong1238 小时前
C# 使用 ZXingCpp.Angle 进行条码、二维码识别
开发语言·c#·扫码
步步为营DotNet8 小时前
System.Text.Json 源生成:三个“看起来已经生效“的静默失效
c#·.net·native aot·源生成
用户788477316341 天前
用 C# 把每周 6 小时的 PDF 录入干到 20 分钟:一份可复制的桌面 OCR 方案
c#
csdn_aspnet1 天前
C# .NetCore Swagger ISchemaFilter 处理枚举、特殊类型
c#·.netcore·swagger
滴滴答答哒1 天前
C# 技巧:优雅实现对象的“局部更新”与“字段合并”
开发语言·c#
code_long1 天前
.net core 连接打印机 使用zpl指令打印机,并且位图本地渲染zpl指令
c#·.netcore·位图·打印机·模板编辑·模板预览
csdn_aspnet1 天前
C# .NetCore Swagger ISchemaFilter Long 转 String 防精度丢失
c#·.netcore·swagger