【问题标题】:virtual keyboard in WPF applicationWPF 应用程序中的虚拟键盘
【发布时间】:2023-03-13 18:35:01
【问题描述】:

我正在使用虚拟键盘做一个 WPF 应用程序。如图中提到的,有两个文本框 pseudopassword,我想使用虚拟键盘输入它们的值。

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

那么我该怎么做呢?

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;
    }
}

【问题讨论】:

  • 我认为您应该以某种方式指定要编写的文本框。

标签: c# wpf xaml keyboard


【解决方案1】:

你应该有这样的东西:

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 处理程序中。

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;
}

【讨论】:

  • 我试过这个private void pseudo_FocusableChanged(Object sender, DependencyPropertyChangedEventArgs e) { MessageBox.Show("pseudo"); isPseudoFocused = true; isPasswordFocused = false; } 但没有显示
【解决方案2】:

您可以为此使用Keyboard.FocusedElement

【讨论】:

  • 是的,但是当您单击按钮时,它会显示按钮已聚焦。
  • 是的,因为当您单击其中一个按钮时,该按钮获得焦点。您需要保留对最后一个焦点文本框的引用,然后使用键盘按钮单击事件将单个字符添加到另一个输入字段。
  • @paul 我做了if(Keyboard.FocusedElement != null) messagebox.show("ok") 但什么也没出现
猜你喜欢
  • 2013-06-07
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多