简体   繁体   English

在屏幕键盘上显示鼠标左键按下PasswordBox,并在框外单击时应隐藏

[英]Show On Screen Key Board on mouse left button down on PasswordBox and should Hide when click outside the box

Platform: C# WPF 平台: C#WPF

Environment: Visual Studio 2013 环境: Visual Studio 2013

Question # 1 : I want to show third party on screen keyboard on mouse left button down on PasswordBox control of C# WPF. 问题1 :我想在C#WPF的PasswordBox控件的鼠标左键上显示屏幕键盘上的第三方。 I used the following code: 我使用了以下代码:

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

But it does not start on screen key board. 但它不会在屏幕键盘上启动。 Instead it triggers on MouseDoubleClick and GotFocus events. 相反,它会在MouseDoubleClickGotFocus事件上触发。

Question # 2 : 问题#2

I want to Hide on screen keyboard when mouse click outside the PasswordBox and Show again on mouse left button down inside box. 我想在PasswordBox框外单击鼠标时在屏幕键盘上隐藏,然后在鼠标左键单击框内再次显示。

Question # 3 : 问题#3

I want to show keyboard on single click instead of mouse double click 我想在单击时显示键盘而不是鼠标双击

I belive the best way to do this will be through using the Focus events, as you only want the keyboard when you're interacting with the PasswordBox , and for it to go once you have stopped interacting. 我相信最好的方法是使用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();
}

You could handle the PreviewMouseLeftButtonDown event for the parent window. 您可以处理父窗口的PreviewMouseLeftButtonDown事件。 Something like this: 像这样的东西:

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;

    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM