1

I'm trying to register certain hotkeys, but I can't because they are Windows defaults.

CTRL+WIN+1 minimizes the current window. I'd like it to do something else.
I'd like to completely disable WIN+LEFT/RIGHT.
I'm also trying to handle the CTRL+WIN+Arrow in my own virtual desktop manager.

zVirtualDesktop

This has to be done using c# and Win32 API if necessary. It absolutely cannot use Autohotkey.

Every page I find descibes how this can be done with AutoHotKey.

I'd post code, but I really don't know where to start. I use Win32 to register hotkeys. I assume there is a way to override them, but I can't find any info.

Does anyone have an idea?

Michael Z.
  • 1,453
  • 1
  • 14
  • 21
  • Is this an example of a _[XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)_? Are you trying to create a kiosk application? – MickyD Apr 07 '17 at 02:29
  • No, on a personal level, I can't stand WIN+LEFT/RIGHT. Also, I'm working on this...https://github.com/mzomparelli/zVirtualDesktop – Michael Z. Apr 07 '17 at 02:31
  • I took the code down for now. I might put it back up. All the commits are still there. – Michael Z. Apr 07 '17 at 02:32
  • I seriously cannot find any info other than AHK and I need to use C#. I didn't know where else to post this. I'm sure this question will get hit by curious people. – Michael Z. Apr 07 '17 at 02:35
  • I understand that I'm lacking stuff here. The obstacle I face is clearly stated and I've searched everywhere except AHK source code. – Michael Z. Apr 07 '17 at 02:41
  • You might be able to install a global keyboard hook and gobble up the key as it comes along? – MickyD Apr 07 '17 at 03:09
  • That might be the only approach and is probably what AHK is doing. – Michael Z. Apr 07 '17 at 03:14
  • @MickyD I totally see why you asked if i was making a kiosk. lol. I just found this....https://stackoverflow.com/questions/43260744/how-do-you-disable-system-hotkeys-in-user32-dll/43303029#43303029 – Michael Z. Apr 09 '17 at 04:30
  • 1
    Oh lol. I wasn't snooping on you :) EDIT: Ohh you just answered that other question too. Nice one :) – MickyD Apr 09 '17 at 04:54
  • oh I just figured you thought I was that guy and just creating a new account or something. Anyway, you were right about the keyboard hook. – Michael Z. Apr 09 '17 at 04:56
  • Not a problem good sir – MickyD Apr 09 '17 at 04:57

1 Answers1

7

It is possible to do this using a keyboard hook. A good hook class for this can be found on this CodeProject Article

Using the below code will prevent the WIN+LEFT or WIN+RIGHT from occurring. You can use this to override whichever keys you'd like.

This will even override hotkeys which you added via RegisterHotKey Win API.

Once you have those classes in your project you can add handlers to the static HookManager class like below.

//It's worth noting here that if you subscribe to the Key_Press event then it will break the international accent keys.
HookManager.KeyPress += HookManager_KeyPress;
HookManager.KeyDown += HookManager_KeyDown;
HookManager.KeyUp += HookManager_KeyUp;

You can also add mouse events, but for simplicity I'm just showing the keyboard hook.

I've also created a generic list so that I know which keys are currently down and I remove those keys from the list on the KeyUp event.

public static List<Keys> keysDown = new List<Keys>();
private static void HookManager_KeyDown(object sender, KeyEventArgs e)
        {
            //Used for overriding the Windows default hotkeys
            if(keysDown.Contains(e.KeyCode) == false)
            {
                keysDown.Add(e.KeyCode);
            }

            if (e.KeyCode == Keys.Right && WIN())
            {
                e.Handled = true;
                //Do what you want when this key combination is pressed
            }
            else if (e.KeyCode == Keys.Left && WIN())
            {
                e.Handled = true;
                //Do what you want when this key combination is pressed
            }

        }

        private static void HookManager_KeyUp(object sender, KeyEventArgs e)
        {
            //Used for overriding the Windows default hotkeys
            while(keysDown.Contains(e.KeyCode))
            {
                keysDown.Remove(e.KeyCode);
            }
        }

        private static void HookManager_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Used for overriding the Windows default hotkeys

        }

        public static bool CTRL()
        {
            //return keysDown.Contains(Keys.LShiftKey)
            if (keysDown.Contains(Keys.LControlKey) || 
                keysDown.Contains(Keys.RControlKey) || 
                keysDown.Contains(Keys.Control) || 
                keysDown.Contains(Keys.ControlKey))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool SHIFT()
        {
            //return keysDown.Contains(Keys.LShiftKey)
            if (keysDown.Contains(Keys.LShiftKey) || 
                keysDown.Contains(Keys.RShiftKey) ||
                keysDown.Contains(Keys.Shift) ||
                keysDown.Contains(Keys.ShiftKey))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool WIN()
        {
            //return keysDown.Contains(Keys.LShiftKey)
            if (keysDown.Contains(Keys.LWin) || 
                keysDown.Contains(Keys.RWin))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool ALT()
    {
        //return keysDown.Contains(Keys.LShiftKey)
        if (keysDown.Contains(Keys.Alt) ||
            keysDown.Contains(Keys.LMenu) ||
            keysDown.Contains(Keys.RMenu))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
Michael Z.
  • 1,453
  • 1
  • 14
  • 21