8 Replies - 5060 Views - Last Post: 08 January 2013 - 06:54 PM Rate Topic: -----

#1 coolandy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 23-September 11

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

Posted 03 January 2013 - 08:25 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: C# Ctrl + Esc disabled but cannot enable the Esc key

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4480
  • View blog
  • Posts: 7,803
  • Joined: 08-June 10

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

Posted 03 January 2013 - 09:24 AM

I'm not an expert in this, but I've been looking at the struct:

http://msdn.microsof...7(v=vs.85).aspx

And it seems that the struct only tracks whether ALT is down or not. You'd have to manually track the state of CTRL yourself. You'll get the WMKEYDOWN and WMKEYUP messages for CTRL, so you have to store the state each time CTRL is pressed or released. Then when the WMKEYDOWN for ESC is sent, check your stored state for CTRL. If it's up, CallNextHookEx. If it's down, don't.

Also, you can take a look at my github for a cleaner way to handle the Low Level Hooks stuff. I won't link it here, but you can just search my name there. You'd have to modify it to suit your purpose, but it might help.
Was This Post Helpful? 1
  • +
  • -

#3 coolandy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 23-September 11

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

Posted 03 January 2013 - 10:50 AM

Ok thanks,
I have tried handling CTRL key pressing during WMKEYDOWN and WMKEYUP messages. Flagged by Boolean ctrl_pressed. If it is false it calls CallNextHookEx otherwise it returns 1.
However it only appears to suppress holding Ctrl then pressing ESC or Holding ESC then pressing Ctrl. If you press Ctrl and Esc at exactly the same time, then start menu appears.
Should I use a different method, or call keyboard hooks only after a WM_KEYDOWN message?

Thank you,

Andy
Was This Post Helpful? 0
  • +
  • -

#4 coolandy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 23-September 11

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

Posted 04 January 2013 - 04:33 AM

I have solved it with this code. I was previously using PreFilterMessage from class IMessageFilter

        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 (wParam == 257) // key up message
                    {
                        if (lParam.vkCode == 162) // control key up
                        {
                            ctrl_pressed = true;
                        }
                    }

                    //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


                    Console.WriteLine("[{0}]>>>[{1}]",lParam.vkCode,lParam.flags);

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


                    break;
            }



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



Was This Post Helpful? 0
  • +
  • -

#5 coolandy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 23-September 11

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

Posted 04 January 2013 - 05:01 AM

Sorry this part should be:
            if (wParam == 257) // key up message
            {
                if (lParam.vkCode == 162) // control key up
                {
                    ctrl_pressed = false;
                }
            }



Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,099
  • Joined: 05-May 12

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

Posted 04 January 2013 - 06:15 AM

I hope that you know that by disabling those keys, you are going against the Microsoft Windows UI guidelines because Windows users expect those keys to work.

Imagine writing a program that disables the Win-C key that brings up the charms bar. Now you've effectively neutered a large portion of what people expect to use to be able to share, print, or change settings.
Was This Post Helpful? 0
  • +
  • -

#7 coolandy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 23-September 11

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

Posted 07 January 2013 - 02:55 AM

Yes I realise this but is required for our client and many of other programs (C#, C++, C) do a Windows lockdown feature.
Was This Post Helpful? 0
  • +
  • -

#8 coolandy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 23-September 11

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

Posted 07 January 2013 - 03:02 AM

Yes I do realise this but is required for our client and many of other programs (C#, C++, C) already do a Windows "Lockdown" feature.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,099
  • Joined: 05-May 12

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

Posted 08 January 2013 - 06:54 PM

<soapbox>
And this is why Mac users say that all their application run with a consistent UI, but Windows users complain that there is no consistency UI experience... Mac users and developers will give a developer grief for no following suit with what Apple has blessed as the UI experience, while Windows developers accept it as the price of doing business to do something that goes against the Windows UI guidelines, and Windows users expect to non-standard UI experiences when it's their program.
</soapbox>
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1