Trouble detecting the Ctrl key

Nov 6, 2011 at 3:55 AM

Hi guys, great stuff you've got here.  Hopefully I am missing something easy and don't take up too much of anyone's time.

I can't seem to get the KeyEventArgs e.Control property to work.  When I press the Ctrl key the KeyDown event is fired but this property shows false, this includes both when the Ctrl key is pressed by itself and when it is held down in conjunction with other key presses.  First, I do have one question: where in the source code does this value get set to be passed to my event handler?  Even searching for the string that appears in the flyover doesn't bring me to that location in your code.

First, I'll describe my setup.  Win 7 Pro x64, compiling the code in x86, using Visual Studio C# 2008 Express with V3 code I downloaded today (11/5/2011).  I extracted your source code as a project then added it to my VS solution (right-click solution in Solution Explorer > Add > Existing Project) then added the reference to it in my project (right-click References under my new project in Solution Explorer, Projects tab, selected your project).  I did get a warning that the target framework for your code was higher than the required framework for mine, this is a bit of a red flag for me so let me know if this is the problem.

I created the following code from scratch.  First I followed the steps above to create the project, then wrote the following for testing.  The variable isCtrlDn will never be updated (i.e. setting a breakpoint inside the IF statement in the event handler will never pause the code as e.Control is never true).  This also applies to the other special properties of e.Alt and e.Shift.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;

namespace GlobalKeyboardHookTest
    public partial class Form1 : Form
        KeyboardHookListener keyboard;
        bool isCtrlDn;

        public Form1()

            keyboard = new KeyboardHookListener(new GlobalHooker());
            keyboard.Enabled = true;
            keyboard.KeyDown += new KeyEventHandler(ProcessKeyDown);

        void ProcessKeyDown(object sender, KeyEventArgs e)
            if (e.Control)
                isCtrlDn = e.Control;

Nov 6, 2011 at 10:04 PM

Correct initialization of  e.Control seems to be an issue.
As a workaround to detect a key combination I would suggest the code snippet from

Just use Keys.Control instead of Keys.LWin as in the sample.


Nov 6, 2011 at 11:43 PM
Edited Nov 6, 2011 at 11:48 PM

That worked perfectly, thank you!  I feel a bit newbish (which I am) because I hadn't noticed that the type for e.KeyCode was System.Windows.Forms.Keys.

I have one more question, perhaps I should start it in another thread (if so please let me know) - is it currently possible to suppress the scroll wheel?  I noticed that adding the code below seemed to catch the mouse scroll sometimes.  It seemed flaky though, if I did a check on ((e.Delta != 0) && isCtrlDn) it never seemed to make it inside my IF statement, but if I put a breakpoint on the IF itself and then stepped through it, it would go inside the IF statement just fine.  Setting it up using mouse.MouseWheel works exactly as intended with the same code except that I can't suppress the scroll (e does not have the property for Handled).  Any thoughts?


The event I tried hooking into:

mouse.MouseMoveExt += new EventHandler<MouseEventExtArgs>(ProcessMouseWheel);

This one works:

mouse.MouseWheel += new MouseEventHandler(ProcessMouseScroll);


private void ProcessMouseScroll(object sender, MouseEventArgs e)
            if ((isLAltDn || isRAltDn) && e.Delta != 0)
                //do something, works for the MouseWheel event

                //however if the event is MouseMoveExt setting a breakpoint in here will never go off

(note: I did have to change the type of e in ProcessMouseScroll from MouseEventArgs to MouseEventExtArgs to use it with MouseMoveExt, just mentioning this for completeness)