Detection Ctrl + Enter

(using WPF) I'm trying to detect when Ctrl + Enter hits. so I tried this code:

if (e.Key == Key.Return && (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)) { //Do Something } 

Obviously, this is not true, since it does not work. Can someone help me by explaining what the correct path should be?

thanks

+7
source share
5 answers

Obviously, e.Key cannot be equal to more than one value in the same event.

You need to handle one of the events using KeyEventArgs , there you will find properties such as Control and Modifiers that will help you detect combinations.

A KeyPress event using KeyPressEventArgs simply does not have enough information.


Drat, you said that WPF is not you. Sounds like you need e.KeyboardDevice.Modifiers .

+13
source

I think you need a special manipulator. I was looking a bit for a solution search here.

The following code from the specified link may solve your problem:

  void SpecialKeyHandler(object sender, KeyEventArgs e) { // Ctrl + N if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.N)) { MessageBox.Show("New"); } // Ctrl + O if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.O)) { MessageBox.Show("Open"); } // Ctrl + S if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.S)) { MessageBox.Show("Save"); } // Ctrl + Alt + I if ((Keyboard.Modifiers == (ModifierKeys.Alt | ModifierKeys.Control)) && (e.Key == Key.I)) { MessageBox.Show("Ctrl + Alt + I"); } } 
+6
source
 if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Enter) 
+3
source
  if (e.KeyChar == 10) { ///Code } 

or

  if ((Char)e.KeyChar == '\n') { ///Code } 
0
source

this method works fine ... but does it get me 2 times?

My code: I want to create a button click // do what on the button event

  if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.Enter)) { Button_Click_Save(sender, e); } 
0
source

All Articles