5

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

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?

4

3 に答える 3

14

キーバインド入力を使用することを好みます。

 <TextBox>
      <TextBox.InputBindings>
         <KeyBinding Key="ENTER" Modifiers="Shift" Command="{Binding YoutCommand}"/>
      </TextBox.InputBindings>
 </TextBox>
于 2012-09-14T15:05:33.840 に答える
13

私はWinFormsとは混ぜません。試す:

<TextBox KeyDown="TextBox_KeyDown" />

このイベント ハンドラーで:

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");
        }
    }
}
于 2012-09-14T14:28:02.053 に答える