A Problem about TextBox.AcceptsReturn Property and KeyDown Event RRS feed

  • Question

  • How can I implement the following animation effects:
    - A multi-line TextBox.
    - When pressing Enter, submit the text.
    - When pressing Ctrl + Enter, insert a newline.

    private bool _isCtrlKeyPressed;
    private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
        if (e.Key == Windows.System.VirtualKey.Control)
             _isCtrlKeyPressed = true;
        if (!_isCtrlKeyPressed && e.Key == Windows.System.VirtualKey.Enter)
            BindingExpression binding = (InputTextBox as TextBox).GetBindingExpression(TextBox.TextProperty);
            var command = (DataContext as MainViewModel).InputText.SubmitCommand;
            if (command.CanExecute(null))
        System.Diagnostics.Debug.WriteLine("Down:" + e.Key);
    private void TextBox_KeyUp(object sender, KeyRoutedEventArgs e)
        if (e.Key == Windows.System.VirtualKey.Control)
            _isCtrlKeyPressed = false;
        System.Diagnostics.Debug.WriteLine("Up:" + e.Key);    

    The problem is that:
    If TextBox.AcceptsReturn property is set to True, KeyDown event will not be raised.

    Friday, October 16, 2015 12:12 PM


  • Hi LapisDev,

    Welcome to the Developing Universal Windows apps forum!

    As a friendly reminder please make sure to add the appropriate tags to the title of your post as per Guide to posting: subject line tags

    I can reproduce your issue in my side, once I set the TextBox.AcceptsReturn property as True, when I press the "enter", the KeyDown event will not be raised. In my mind it is because that KeyDown event is a routed event, which means it will first be raised on the source control (TextBox)->then on the parent->then on the parent's parent, until it is handled. When we set AcceptsReturn to true, the control handles the Enter key, so the event is not handled.
    One workaround for your issue is to set the TextBox.AcceptsReturn property as false and try to implement the insert a newline function in code behind.

    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 19, 2015 8:43 AM