WPF应用程序中的虚拟键盘 [英] virtual keyboard in WPF application

查看:404
本文介绍了WPF应用程序中的虚拟键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用虚拟键盘执行WPF应用程序.如图片所示,有两个文本框pseudopassword,我想使用虚拟键盘输入它们的值.

I am doing a WPF application with a virtual keyboard .As mentionned in the picture, there is two textbox pseudo and password and i'd like to enter their values using the virtual keyboard.

问题是如何知道光标在第一个字段中还是第二个字段中或外.我尝试了isfocused,但没有得到结果.

The problem is how to know that the cursor is in the first field or in the second one or out. I tried isfocused but it didn't give a result.

那我该怎么做呢?

public partial class Authentification : Window
{
    public TextBox numero = new TextBox();
    bool isPseudoFocused = false;
    bool isPasswordFocused = false;
    public Authentification()
    {
        InitializeComponent();
        WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        if (Keyboard.FocusedElement == pseudo)
            MessageBox.Show("hhhh");
    }


    private void un_Click(object sender, RoutedEvent e)
    {
        if (isPseudoFocused) pseudo.Text += "1";
        if (isPasswordFocused) password.Text += "1";
    }
    private void pseudo_FocusableChanged(Object sender, DependencyPropertyChangedEventArgs e)
    {
        MessageBox.Show("pseudo");
        isPseudoFocused = true;
        isPasswordFocused = false;
    }
    private void password_FocusableChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        MessageBox.Show("password");
        isPseudoFocused = false;
        isPasswordFocused = true;
    }
}

推荐答案

您应该具有以下内容:

bool isPseudoFocused = false;
bool isPasswordFocused = false;

//when Pseudo gets focus the set 
isPseudoFocused = true; 
isPasswordFocused = false;

//when Password gets focus then set
isPseudoFocused = false; 
isPasswordFocused = true;

//when you are typing text then you know where to put your text.

更新:

您应该将此代码放入TextBox_GotFocus处理程序中.

you should put this code into a TextBox_GotFocus handler.

private void pseudo_GotFocus(object sender, RoutedEventArgs e)
{
    MessageBox.Show("pseudo"); isPseudoFocused = true; isPasswordFocused = false;
}
private void password_GotFocus(object sender, RoutedEventArgs e)
{
    MessageBox.Show("password"); isPseudoFocused = false; isPasswordFocused = true;
}

这篇关于WPF应用程序中的虚拟键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆