469,304 Members | 2,303 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

How to handle Ctrl+Enter

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"
Nov 16 '05 #1
3 22469
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

Coder wrote:
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"

Nov 16 '05 #2
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

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
10x a lot !!
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Coder | last post: by
1 post views Thread by Stephen J. Shephard | last post: by
4 posts views Thread by sravan_reddy001 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.