WPF:使用虚拟键盘 [英] WPF: Using a Virtual Keyboard

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

问题描述

我创建了一个虚拟键盘用户控件,以便在我的应用程序中的多个窗口中使用.我想知道如何在按下某个键时将其输入到窗口中的文本框中.

I have created a virtual keyboard user control to use across multiple windows within my application. I am wondering how I am able to get it to input into a textbox in a window when a key is pressed.

我正在寻找的是这样的:

What I am looking for is something like:

private void keyboardKey_Click(object sender, RoutedEventArgs e){
var key = sender as Button;
textbox.Text += key.Content;
}

例如,如果我按下a"键,则a"会添加到文本框中.

So for example, if I press the key for 'a', then 'a' is added to the textbox.

我的想法倾向于某种绑定属性,但由于我是 WPF 的新手,我不知道从哪里开始.类似的东西

My mind tends towards some sort of binding property but as I am new to WPF, I have no idea where to begin. Something like

<local:QWERTYKeyboard TextboxBinding="TextboxName"/>

谢谢

推荐答案

这是一项相当复杂的任务.幸运的是,关于这个主题有几个很好的教程.

This is quite complicated task. Fortunately there's couple good tutorials on this subject.

我建议您阅读以下两个教程:

I would recommend you to go through these two tutorials:

特别是第一个应该包含一个示例应用程序,可以帮助您入门.

Especially there first one should contain a sample app which should get you started.

关于将文本放入 TextBox 的特定问题.一种(幼稚的)实现是跟踪焦点.

For your particular question regarding getting the text into TextBox. One (naive) implementation is to track the focus.

您的虚拟键盘可能具有包含当前焦点文本框的属性:

Your virtual keyboard could have property which contains the currently focused TextBox:

public TextBox FocusedTextBox {get;set;}

并且您应用的每个文本框都可以根据 GotFocus 事件更新属性:

And each of your app's textboxes could update the property based on the GotFocus-event:

private void txtBox_GotFocus(object sender, RoutedEventArgs e)
{
    // Set virtual keyboards' active textbox
    this.VirtualKeyboard.FocusedTextBox = txtBox;
}

现在在您的虚拟键盘中,当您按下a"时,您可以更新 TextBox 的内容:

Now in your Virtualkeyboard, when one presses "a", you can just update the content of the TextBox:

private void UserPressedVirtualKeyboard(object sender, RoutedEventArgs e)
{
    this.VirtualKeyboard.FocusedTextBox.Text = this.VirtualKeyboard.FocusedTextBox.Text + pressedChar;
}

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

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