简体   繁体   English

如何在wpf应用程序中创建键盘输入?

[英]How do I create keyboard input in a wpf application?

I want to make a wpf application in c# that displays some text on screen, and where the user is supposed to write a response and press enter to submit the response. 我想用c#制作一个wpf应用程序,该应用程序在屏幕上显示一些文本,并且用户应该在其中编写响应,然后按Enter提交响应。 I don't want to use a textbox, since there is only one line for the text input in the window, and I don't want the user to have to click to select the textbox. 我不想使用文本框,因为窗口中输入的文本只有一行,并且我不想用户必须单击以选择文本框。 I want the application to be mouse-free. 我希望该应用程序没有鼠标。

My question is: How do I make it so that when the user has written their answer, they can submit the response simply by pressing enter? 我的问题是:如何做到这一点,以便用户写下答案后,只需按Enter即可提交响应?

I have tried the following snippet of code which I found on a microsoft help website: 我已经尝试在Microsoft帮助网站上找到以下代码片段:

private void OnKeyDownHandler(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Return)
        {

            doSomething();
        }
    }

I suppose I have to add some code elsewhere, but I'm not sure where or what I need to add. 我想我必须在其他地方添加一些代码,但是我不确定我需要在哪里添加什么。

If you have a button that you're using for submit, you can easily set it as the default by using the IsDefault=true (wrote a tip about doing this and the cancel for the escape here .) 如果您有用于提交的按钮,则可以通过使用IsDefault=true轻松地将其设置为默认按钮(在此处写了有关执行此操作的提示以及取消转义的提示)。

Other than that, you'll have to have somewhere to write it (yet you don't want a textbox? you can select it by default, or tab into it if you don't have the focus there), and you can handle the keydown to "catch" the Enter otherwise. 除此之外,您还必须在某个地方编写它(但是,您是否不想使用文本框?您可以默认选择它,或者如果您没有焦点,可以选择进入它),并且可以处理Enter key键以“捕获” Enter否则。

If you want to make sure your window process every Enter key press without care what control is focused you can use PreviewKeyDown event: 如果要确保每次按下Enter键时窗口都处理,而无需关注控件的焦点,则可以使用PreviewKeyDown事件:

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        //Process user input
        e.Handled = true;
    }
} 

Of course if you are doing mvvm you can create a behavior to encapsulate the event handler: 当然,如果您正在执行mvvm,则可以创建行为来封装事件处理程序:

 public class WindowBehavior : Behavior<Window>
    {
        protected override void OnAttached()
        {
            AssociatedObject.PreviewKeyDown += AssociatedObject_PreviewKeyDown;
        }

    private void AssociatedObject_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            MessageBox.Show("Enter from Window");
            e.Handled = true;
        }
    }

    protected override void OnDetaching()
    {
        AssociatedObject.PreviewKeyDown -= AssociatedObject_PreviewKeyDown;
    }

I suggest you to read this article about bubble, tunneling and direct events basic for WPF events. 我建议您阅读有关WPF事件基本的气泡,隧道和直接事件的文章

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

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