2

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. 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?

I have tried the following snippet of code which I found on a microsoft help website:

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.

4
  • " I don't want to use a textbox, since there is only one line for the text input in the window"... so what are you using? Commented Jul 4, 2015 at 14:43
  • Maybe a textblock or a label. But on second thought, I might use a textbox, as long as accidentally pressing the mouse doesn't deselect the textbox. Commented Jul 4, 2015 at 15:36
  • But, do you require the user to type in something before pressing enter key or not? Commented Jul 4, 2015 at 16:14
  • Yes, the user types a response and then presses enter, and then some new text appears and so on. Commented Jul 4, 2015 at 20:46

2 Answers 2

3

If you want to make sure your window process every Enter key press without care what control is focused you can use PreviewKeyDown event:

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:

 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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.)

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.

2 Comments

"and you can handle the keydown to "catch" the Enter otherwise." What do you mean by this?
You can handle the keyboard event on the textbox, and show the next question when enter was pressed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.