Re: How to handle Ctrl+Enter

From: Morten Wennevik (MortenWennevik_at_hotmail.com)
Date: 03/06/05


Date: Sun, 06 Mar 2005 16:27:21 +0100

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]


Relevant Pages

  • Re: Since chvt works, the problem must be the keymap
    ... if you run "xev", press both CTRL and ALT, and then press F1, ... etc. Does xev really display the keycodes for the Fn keys and the ... KeyPress event, serial 29, synthetic NO, window 0x2600001, ... ALT before you press F1, you still get the normal "F1" keysymbol and not ...
    (Debian-User)
  • Re: M as shortcut key
    ... You mean without using Shift, Ctrl, or Alt? ... Not from the VB IDE, ... could check for it in the KeyPress event though, and you could put it on ...
    (microsoft.public.vb.controls)
  • Re: Delete Key behavior
    ... Are you processing text in a textbox using the KeyPress event? ... The delete key works in textboxes in VB6. ... Morten Wennevik [C# MVP] ...
    (microsoft.public.dotnet.general)
  • Re: keyPress event on dataGrid Coulmns
    ... Nicholas Paldino [.NET/C# MVP] ... > how can i make a different keypress event for each coulmn in dataGrid ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: What is the hex value of the enum WM_KEYPRESS?
    ... the Api equivalent to VB's KeyPress event, ... MVP - Visual Basic ... (please post replies to the newsgroup) ...
    (microsoft.public.vb.winapi)