Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to know if Ctrl is pressed with "C" twice fast so my program speaks the text in clipboard.

What I have tried:

static globalKeyboardHook gkh = new globalKeyboardHook();
static void gkh_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {


                case Keys.LControlKey:
                    LCTRLPRESSED = false; break;
             }
        }

static void gkh_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {


                case Keys.LControlKey:
                    LCTRLPRESSED = true; break;
             }
        }


But this sometimes causes my program to speak the text without Ctrl being pressed when "C" is pressed twice fast, as Ctrl lags as being down till I press it again and it updates to being up.

There's:
if(e.Control) {;}

But it doesn't work correctly.

There's also e.Modifiers, does it work? if yes how?
Posted
Updated 13-Nov-20 11:57am
v2

1 solution

If you would like to detect if CTRL+C was pressed, then you have to use:

C#
if (e.KeyCode == Keys.C && e.Modifiers == Keys.Control)  {
    MessageBox.Show("You pressed CTRL+C");
}
 
Share this answer
 
Comments
john1990_1 13-Nov-20 8:05am    
Thx but sorry even when I press Left or Right Ctrl it still gives me:
Modifiers as "None".
Maciej Los 13-Nov-20 9:26am    
Have you added CTRL key to gkh?
gkh.HookedKeys.Add(Keys.LControlKey);

Please, follow below link for further details: A Simple C# Global Low Level Keyboard Hook[^]
john1990_1 13-Nov-20 9:37am    
I did everything as described, I even used the source code you linked to and when I added:
if (e.Modifiers == Keys.Control) {;}
It never got inside the brackets and even while I'm still holding LCtrl it the Modifiers said: "None"

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