Articles

Solved C# Ctrl + Esc disabled but cannot enable the Esc key

January 3, 2013 at 07:10:14
Specs: Windows 7, 4GB

I have following the function which disables windows key, alt-tab, alt+esc, ctrl+esc etc:

        public static int HookCallback(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            bool blnEat = false;

            switch (wParam)
            {
                case 260:
                case 256:
                case 257:
                case 261:
                    //Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key,
                    blnEat = ((lParam.vkCode == 9) && (lParam.flags == 32))  // alt+tab
                             |
                             ((lParam.vkCode == 27) && (lParam.flags == 32)) // alt+esc
                             |
                             ((lParam.vkCode == 27) && (lParam.flags == 0))  // ctrl+esc
                             |
                             ((lParam.vkCode == 91) && (lParam.flags == 1))  // left winkey
                             |
                             ((lParam.vkCode == 92) && (lParam.flags == 1));

                    break;
            }

            if (blnEat == true)
            {
                return 1;
            }
            else
            {
                return CallNextHookEx(0, nCode, wParam, ref lParam);
            }
        }

This line of code disables Ctrl + Esc (which brings up the start menu)

((lParam.vkCode == 27) && (lParam.flags == 0))  

Unfortunately this disables the Esc key on its own. I actually want to be able to use the ESC key on its own but have the problem of also requiring Ctrl+Esc combination to be disabled. Is there another method I could use?

Thanks,

Andy


See More: C# Ctrl + Esc disabled but cannot enable the Esc key

Report •


#1
January 7, 2013 at 02:06:30
✔ Best Answer

Solved with this code:

        public static bool ctrl_pressed = false;
        public static bool alt_pressed = false;

        public static int HookCallback(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            bool blnEat = false;

            switch (wParam)
            {
                case 260:
                case 256:
                case 257:
                case 261:

                    if (wParam == 256) // key down message
                    {
                        if (lParam.vkCode == 162) // control key down
                        {
                            ctrl_pressed = true;
                        }

                        if (lParam.vkCode == 164) // alt key down
                        {
                            alt_pressed = true;
                        }
                    }

                    if (wParam == 257) // key up message
                    {
                        if (lParam.vkCode == 162) // control key up
                        {
                            ctrl_pressed = false;
                        }

                        if (lParam.vkCode == 164) // alt key up
                        {
                            alt_pressed = false;
                        }
                    }

                    //Alt+Tab, Alt+Esc, Windows Key,
                    blnEat = ((lParam.vkCode == 9) && (lParam.flags == 32))  // alt+tab
                             |
                             ((lParam.vkCode == 27) && (lParam.flags == 32)) // alt+esc
                             |
                             ((lParam.vkCode == 91) && (lParam.flags == 1))  // left winkey
                             |
                             ((lParam.vkCode == 92) && (lParam.flags == 1)); // right winkey

                    // Supress control and Esc if required
                    if (ctrl_pressed && !blnEat)
                        blnEat = (lParam.vkCode == 27) && (lParam.flags == 0);

                    // Reset if required after ctrl-alt-del

                    if (ctrl_pressed && alt_pressed && lParam.vkCode == 46 && lParam.flags == 1)
                    {
                        //Console.WriteLine("reset after ctrl+alt+del");
                        ctrl_pressed = false;
                        alt_pressed = false;
                    }

                    break;
            }



            if (blnEat == true)
            {
                return 1;
            }
            else
            {
                return CallNextHookEx(0, nCode, wParam, ref lParam);
            }
        }


Report •
Related Solutions


Ask Question