【问题标题】:Show On Screen Key Board on mouse left button down on PasswordBox and should Hide when click outside the box在密码框上的鼠标左键向下显示屏幕键盘,在框外单击时应隐藏
【发布时间】:2019-05-03 16:58:20
【问题描述】:

平台: C# WPF

环境: Visual Studio 2013

问题 #1:我想在 C# WPF 的 PasswordBox 控件上的鼠标左键上显示第三方屏幕键盘。我使用了以下代码:

private void PasswordBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    System.Diagnostics.Process.Start("D:\\CCOnScreenKeyboard.exe"); 
}

但它不会在屏幕键盘上启动。相反,它会在 MouseDoubleClickGotFocus 事件上触发。

问题 #2

当鼠标在PasswordBox 外单击时,我想在屏幕键盘上隐藏,并在框内鼠标左键按下时再次显示。

问题 #3

我想在单击而不是鼠标双击时显示键盘

【问题讨论】:

  • 您可能需要考虑在您的应用启动时启动该过程,然后仅在需要时更改 OnscreenKeyboard 表单的可见性。这可能会少一些错误。
  • Yes @ Maxter 和鼠标单击而不是双击

标签: c# wpf


【解决方案1】:

您可以处理父窗口的PreviewMouseLeftButtonDown 事件。像这样的:

bool isVisible = false;
PreviewMouseLeftButtonDown += (ss, ee) => 
{
    if (!passwordBox.IsMouseOver && isVisible)
    {
        System.Diagnostics.Process.GetProcessesByName("CCOnScreenKeyboard")?.FirstOrDefault()?.Kill();
    }
    else if (!isVisible)
    {
        System.Diagnostics.Process.Start("D:\\CCOnScreenKeyboard.exe");
        isVisible = true;

    }
};

【讨论】:

    【解决方案2】:

    我相信最好的方法是使用Focus 事件,因为您只需要在与PasswordBox 交互时使用键盘,并且一旦您停止交互就可以使用它。

    private void PasswordBox_GotFocus(object sender, RoutedEventArgs e) => 
        Process.Start("D:\\CCOnScreenKeyboard.exe");
    
    private void PasswordBox_LostFocus(object sender, RoutedEventArgs e)
    {
        foreach (var process in Process.GetProcessesByName("CCOnScreenKeyboard"))
            process.Kill();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 2013-06-10
      • 1970-01-01
      • 2013-06-26
      • 2012-03-26
      相关资源
      最近更新 更多