wpf 处理文本框中的 Shift-Enter 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12426265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:21:28  来源:igfitidea点击:

Handling Shift-Enter event in a textbox

c#wpf

提问by Julien

I want a textbox where the user can shift-enter or ctrl-enter to add a newline without submitting. I found the following post on how to do ctrl-enter

我想要一个文本框,用户可以在其中使用 shift-enter 或 ctrl-enter 添加换行符而无需提交。我找到了以下关于如何执行 ctrl-enter 的帖子

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/67ef5912-aaf7-43cc-bfb0-88acdc37f09c

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/67ef5912-aaf7-43cc-bfb0-88acdc37f09c

works great! so i added my own block to capture shift enter like so:

效果很好!所以我添加了自己的块来捕获移位输入,如下所示:

  else if (((keyData & swf.Keys.Shift) == swf.Keys.Shift) && ((keyData & swf.Keys.Enter) == swf.Keys.Enter) && Keyboard.FocusedElement == txtMessage)
  {
     // SHIFT ENTER PRESSED!
  }

except now the box is capturing other shift combinations such as the question mark and squiggly braces and then adding a newline. What do I need to change to prevent this from happening?

除了现在该框正在捕获其他移位组合,例如问号和波浪形大括号,然后添加换行符。我需要改变什么来防止这种情况发生?

回答by LPL

I wouldn't mix with WinForms. Try:

我不会与 WinForms 混合使用。尝试:

<TextBox KeyDown="TextBox_KeyDown" />

with this event handler:

使用此事件处理程序:

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
        {
            MessageBox.Show("Control + Enter pressed");
        }
        else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift))
        {
            MessageBox.Show("Shift + Enter pressed");
        }
    }
}

回答by ígor

I prefeer use keybinnding inputs:

我更喜欢使用键绑定输入:

 <TextBox>
      <TextBox.InputBindings>
         <KeyBinding Key="ENTER" Modifiers="Shift" Command="{Binding YoutCommand}"/>
      </TextBox.InputBindings>
 </TextBox>

回答by swedish joe

After trying most things i decided to experiment a little my self. And this seems to do the job in an easy

在尝试了大多数事情之后,我决定自己尝试一下。这似乎很容易完成这项工作

In the designer view. Select the textbox and under the "events" lightningbolt doublecklick on key->KeyDown. you will be placed in the code.

在设计师看来。选择文本框并在“事件”下闪电双击 key->KeyDown。您将被置于代码中。

paste this:

粘贴这个:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Shift)
            {
                if(e.KeyCode == Keys.Enter)
                {
                    MessageBox.Show("shift enter");
                }
            }
        }

If the key shift is pressed. and then in that if-statement. if Enter is pressed a messagebox will pop up.

如果按下了键shift。然后在那个 if 语句中。如果按下 Enter 会弹出一个消息框。

Swap the messagebox with your task.

用您的任务交换消息框。