0

I can use Enter key to start a new line but can I use Ctrl+Enter instead?

I want to use Enter key to do other thing.

The KeyDown event I tried not work.

it is always start a new line when I press Enter.

user666
  • 329
  • 1
  • 7
  • 22
  • Can you show code for your event handler of KeyDown? Also, why don't you try KeyUp which triggers after (not during) you press keys. – Everyone May 29 '19 at 05:05

2 Answers2

4

You can set the value for property AcceptsReturn to false and handle the ctrl + enter key by creating an event handler for PreviewKeyDown event like this:

(rtb is name of the RichTextBox)

private void RichTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter && Keyboard.IsKeyDown(Key.LeftCtrl))
    {
        // do whatever you want
        // if u want to add a new line just uncomment the next lines
        // rtb.AppendText(Environment.NewLine);
        // rtb.CaretPosition = rtb.CaretPosition.DocumentEnd;
        e.Handled = true;
    }
}
eren
  • 708
  • 8
  • 20
  • https://stackoverflow.com/questions/5750722/how-to-detect-modifier-key-states-in-wpf for handling modifier keys – eren May 29 '19 at 06:10
1

i feel like for this requirement you should use only enter key it will work according to me

 private void richTextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode==Keys.Enter  )
            {
                  //Enter key is down
                //Capture the text in next line  if you press enter only this event will 
            }
            }
Khadar
  • 79
  • 6