在UWP中将SHIFT + ENTER用于多行文本框 [英] Use SHIFT + ENTER for multiline textbox in UWP

查看:104
本文介绍了在UWP中将SHIFT + ENTER用于多行文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调整TextBox的行为,以便键盘上的SHIFT + ENTER插入新行,而ENTER仅执行一条不同的命令(例如在消息传递应用程序中更改焦点或按下发送" ).

I would like to adjust the behavior of a TextBox, so that SHIFT + ENTER on the keyboard inserts a new line, while just ENTER executes a different command (such as changing focus or pressing "send" like in messaging apps).

默认情况下,当按ENTER键时,将TextBoxAcceptsReturn属性设置为true会插入新行.将AcceptsReturn更改为false似乎完全阻止了新行的工作,即使我手动添加了新行也是如此:

By default, the AcceptsReturn property of the TextBox being set to true inserts a new line when ENTER is pressed. Changing AcceptsReturn to false seems to prevent the new line from working at all, even if I manually add the new line:

private void ContentTextBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
    // NOTE - AcceptsReturn is set to false in XAML.
    if (e.Key == VirtualKey.Enter)
    {
        var keyState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift);
        if ((keyState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down)
        {
            // SHIFT is pressed, so add a new line.
            this.ContentTextBox.Text += "\r";
        }
        else
        {
            // SHIFT is not pressed, so execute my ENTER logic.
            this.Focus(FocusState.Programmatic);
        }
    }
}

基于此帖子,我当时是能够提出一种在功能上可行但具有视觉副作用的变通办法.我将AcceptsReturn设置为true,然后在按下时手动删除新行,然后在按下ENTER时执行我想要的代码.副作用是该文本框将扩展以容纳新行,然后立即再次收缩,这表明该文本框甚至在我的处理程序运行之前就自动处理了ENTER输入.此代码如下:

Based on this post, I was able to come up with a workaround that functionally works, but has a visual side effect. I set AcceptsReturn to true, and then manually remove the new line when SHIFT is not pressed, followed by executing the code I want when just ENTER is pressed. The side effect is that the textbox expands to accommodate the new line, then immediately shrinks again, suggesting that its automatically handling the ENTER input before my handler is even run. The code for this follows:

private void ContentTextBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
    // NOTE - AcceptsReturn is set to true in XAML.
    if (e.Key == VirtualKey.Enter)
    {
        // If SHIFT is pressed, this next IF is skipped over, so the
        //     default behavior of "AcceptsReturn" is used.
        var keyState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift);
        if ((keyState & CoreVirtualKeyStates.Down) != CoreVirtualKeyStates.Down)
        {
            // SHIFT is not pressed, so remove the new line.
            string textboxText = this.ContentTextBox.Text;
            textboxText = textboxText.Remove(textboxText.Length - 1);
            this.ContentTextBox.Text = textboxText;

            // Execute my ENTER logic.
            this.Focus(FocusState.Programmatic);
        }
    }
}

是否有其他方法可以消除这种副作用?我尝试调整e.IsHandled值,但是没有用(如果默认行为在我的代码之前运行,这是有道理的.)

Is there a different way to do this, or a way to get rid of that side effect? I tried adjusting the e.IsHandled value, but that didn't work (which makes sense, if the default behavior is running before my code).

推荐答案

(后接评论)您可以使用 PreviewKeyDown 事件,因为keydown事件不会为系统处理的键触发

(In continuation from comment)You can use PreviewKeyDown Event as keydown event will not fire for system handled keys

  private void TextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down)&& e.Key == Windows.System.VirtualKey.Enter)
            {
               //Add New Line
            }
            else if (e.Key == Windows.System.VirtualKey.Enter)
            {
                //This will prevent system from adding new line
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }

这篇关于在UWP中将SHIFT + ENTER用于多行文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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