Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im trying to do something if CTRL is down, but not if CTRL + C is down, and I don't really get how to do it...

Here's my code:

C#
public Window()
{
    InitializeComponent();
    mskTime.KeyPress += new KeyPressEventHandler(keypressed);
}

private void keypressed(Object o, KeyPressEventArgs e)
{   
    // the C here doesn't work
    if (Control.ModifierKeys == Keys.Control && e.KeyChar != (char)Keys.C)
    {
         e.Handled = true;
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 1-Apr-13 15:54pm    
You always need to tag your application type and UI library you are using (WPF, Forms, ASP.NET, etc...).
—SA
Member 9953050 1-Apr-13 16:02pm    
Oh sorry. It's a usual C# project in Visual Studio, probably default UI then. Hope that helps... I still haven't solved this since I'm very new to this, so if you wish - feel free to give a code example.
Sergey Alexandrovich Kryukov 1-Apr-13 16:05pm    
There is no "default UI". You always choose some certain project from template, with Visual Studio. You should learn what you are using, otherwise you cannot get any help, at least not in UI.
Nevertheless, I provided a general answer you can use right now; please see.
—SA
Member 9953050 1-Apr-13 16:13pm    
I'm using Windows Forms Application if it's that that's the UI. Well thanks for the answer. I tried to change to KeyDown, like:

mskTime.KeyDown += new KeyPressEventHandler(keypressed);

But it won't cope with KeyPressEventHandler, and KeyDownEventHandler just giver errors. Maybe you meant something else?
Sergey Alexandrovich Kryukov 1-Apr-13 16:20pm    
You need to tag: "WinForms".

I already answered. And no, I did not mean anything else. If it give you errors, you screw it up, but how? Due to 1st of April traffic, my access to your hard drive is somewhat limited.

—SA

Then you need to handle not KeyPress, but KeyDown or PreviewKeyDown. I'm not providing more detail because you did not tag UI library you are using or application type, but you will easily find out how to detect Ctrl key using the data passed in event arguments.

[EDIT]

Thank you for clarification. You need to use:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeydown.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpreviewkeydown.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx[^],
or
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx[^].

To get an information of a key, see also: http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx[^].

Use:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keycode.aspx[^].

—SA
 
Share this answer
 
v2
try this :

if ((Control.ModifierKeys == Keys.Control) && ((int)e.KeyChar != 3))
           {
               e.Handled = true;
           }



--------------------

Regards
H.Maadani
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900