How to handle Ctrl+Enter

  • Thread starter Thread starter Coder
  • Start date Start date
C

Coder

hi, there is still no answer, i wonder if there is a way to handle the
multiple key-press event on a C# windows form, like "Ctrl+Enter"
 
Use the KeyDown or KeyUp event. The KeyEventArgs passed to the event
has a property "Control" that tells you whether the Ctrl key was
pressed. You can then check KeyValue to see if Enter was pressed (13?).
Alternatively, you can read KeyData to check for both keys at once using
bitwise OR:

if (e.KeyData == (Keys.ControlKey | Keys.Enter))


Joshua Flanagan
http://flimflan.com/blog
 
Hi Coder,

You can check for Ctrl inside the KeyPress event by using the static
properties Control.ModifierKeys
In theory you should be able to do

if(e.KeyChar == (char)13 && Control.ModifierKeys == Keys.Ctrl)

Except this doesn't work. Modifierkeys are translated to characters
inside the KeyPress event. When you hold ctrl while clicking Enter
(char)10 is sent instead of (char)13 and the Control click is suppressed,
so all you have to do to detect Ctrl+Enter is

if(e.KeyChar == (char)10)

The same goes for other combinations like

if(e.KeyChar == (char)97) // [A]
if(e.KeyChar == (char)1 ) // [CTRL]+[A]

To detect key combinations put something like this inside the KeyPress
event

MessageBox.Show(((int)e.KeyChar).ToString());

In the end, you might be better off using the KeyUp/KeyDown events as
Joshua said
 
Back
Top