当前位置: 首页 > news >正文

WPF自动弹出软件键盘

使用全局类型获取焦点事件

    EventManager.RegisterClassHandler(typeof(TextBox), TextBox.GotFocusEvent, new RoutedEventHandler(TextBox_GotFocus));EventManager.RegisterClassHandler(typeof(TextBox), TextBox.LostFocusEvent, new RoutedEventHandler(TextBox_LostFocus));private void TextBox_GotFocus(object sender, RoutedEventArgs e){//获取焦点打开键盘}private void TextBox_LostFocus(object sender, RoutedEventArgs e){//失去焦点关闭键盘}

NuGet包:Microsoft.Xaml.Behaviors.Wpf

using Microsoft.Xaml.Behaviors;
using System.Diagnostics;
using System.Windows.Controls;public class ShowTouchKeyboardBehavior : Behavior<TextBox>
{protected override void OnAttached(){base.OnAttached();AssociatedObject.GotFocus += AssociatedObject_GotFocus;AssociatedObject.PreviewMouseDown += AssociatedObject_PreviewMouseDown;}protected override void OnDetaching(){base.OnDetaching();AssociatedObject.GotFocus -= AssociatedObject_GotFocus;AssociatedObject.PreviewMouseDown -= AssociatedObject_PreviewMouseDown;}private void AssociatedObject_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e){ShowTouchKeyboard();}private void AssociatedObject_GotFocus(object sender, RoutedEventArgs e){ShowTouchKeyboard();}private void ShowTouchKeyboard(){try{Process.Start(new ProcessStartInfo{FileName = "cmd.exe",Arguments = "/c start tabtip:",WindowStyle = ProcessWindowStyle.Hidden,CreateNoWindow = true});}catch{// 备用方案try{Process.Start("osk.exe");}catch { }}}
}

在XAML中使用:

<Window x:Class="YourNamespace.MainWindow"xmlns:behaviors="clr-namespace:YourNamespace"><Grid><TextBox Width="200" Height="30"><i:Interaction.Behaviors><behaviors:ShowTouchKeyboardBehavior/></i:Interaction.Behaviors></TextBox><PasswordBox Width="200" Height="30" Margin="0,40,0,0"><i:Interaction.Behaviors><behaviors:ShowTouchKeyboardBehavior/></i:Interaction.Behaviors></PasswordBox></Grid>
</Window>

或者另外一种xaml附加法

public static class TouchKeyboardHelper
{public static readonly DependencyProperty EnableTouchKeyboardProperty =DependencyProperty.RegisterAttached("EnableTouchKeyboard", typeof(bool), typeof(TouchKeyboardHelper),new PropertyMetadata(false, OnEnableTouchKeyboardChanged));public static void SetEnableTouchKeyboard(UIElement element, bool value)=> element.SetValue(EnableTouchKeyboardProperty, value);public static bool GetEnableTouchKeyboard(UIElement element)=> (bool)element.GetValue(EnableTouchKeyboardProperty);private static void OnEnableTouchKeyboardChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){if (d is Control control){if ((bool)e.NewValue)control.GotFocus += Control_GotFocus;elsecontrol.GotFocus -= Control_GotFocus;}}private static void Control_GotFocus(object sender, RoutedEventArgs e){string tabTipPath = @"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe";if (File.Exists(tabTipPath) && Process.GetProcessesByName("TabTip").Length == 0)Process.Start(tabTipPath);}
}

XAML里如下使用

<TextBox Width="200"Height="40"local:TouchKeyboardHelper.EnableTouchKeyboard="True" />
http://www.hskmm.com/?act=detail&tid=31688

相关文章:

  • 【碎片化学习】JMeter中常用的设置优化
  • win10系统以太网未识别网络 没有有效ip配置怎么办?
  • 怎么考PostgreSQL PG中级认证证书
  • 大学本科及研究生金融专业题库数据集:109157条高质量中文金融教育题库数据,涵盖银行证券保险投资理财等全领域,支持智能教育系统与机器学习算法训练的专业数据集
  • 【比赛记录】2025CSP-S模拟赛61
  • 基于Rokid CXR-S SDK的智能AR翻译助手技术拆解与实现指南
  • VRED 2025:专业三维可视化与虚拟现实领域的高效设计工具
  • 2025年办公与商业空间软膜天花系统推荐榜:办公室/酒店/展厅/商场/汽车4S店软膜天花厂家,专注光环境与装饰一体化解决方案
  • SZMS 251009 订题赛 题解
  • Debian 12安装docker的正确方法
  • 【流量网关】k8s与apisix统一的流量入口方案(内网版)
  • 基于STM32F4系列MCU和CS5530 24位SDADC的称重传感器系统实现
  • 2025 年环保板材厂家最新推荐榜:硬包板 / 竹木纤维板等全品类 企业深度解析
  • kong 网关下集成 Consul服务注册与发现
  • cad圆滑连接两段线:blend
  • 在 gitea 服务器端查询 lfs 文件占用情况
  • HDR图像生成算法详解
  • Introduction: Why Optimization?
  • 基于MATLAB的二自由度机械臂PID控制仿真
  • Spring AOP原理
  • Ventoy引导Kali live USB持久化
  • 知识库管理工具深度测评:ONES、Confluence 等10款工具全面对比
  • 好的测试数据管理,到底要怎么做?
  • 【面试题】人工智能工程师高频面试题汇总:循环神经网络篇(题目+答案)
  • 做了个手机上的“视频播放器”,获益匪浅
  • CEF关闭流程
  • AI一周资讯 251005-251015
  • 2025 年中空百叶源头厂家最新推荐排行榜:聚焦国内优质供货商,助力客户精准选购可靠产品光能/光伏/电动/光动中空百叶厂家推荐
  • 2025年学校家具定制厂家最新权威推荐榜:全屋定制/衣柜/厨柜/酒柜/鞋柜/猫柜/酒店办公家具/电视柜/书包柜/图书架/宿舍上下床
  • iOS框架内存中占用很高的ttc文件是否正常