Question Global hotkeys with hooks

Sinesters

New member
Joined
Sep 28, 2019
Messages
1
Programming Experience
Beginner
I'm doing a overlay for a video game, and on the last stage i wanted to add global hotkeys, that work when not focused, so when we are running the game we can minimize and then restore, i got it working using `RegisterHotKey` method but this method dons't work when the game is loaded so i need hook, i can see in the debug that he knows i pressed the keys, but now i'm having trouble making it work whit the actions that i want, i leave here the code.

Problabyis a easy fix but im rather new and i have been learning developing this little overlay, but i cant figure this one out.



C#:
private const uint SW_RESTORE = 0x09;
        IntPtr thisWindow;
    
        public Menu()
        {
            InitializeComponent();
    
        }
    
    
        private void Menu_Load(object sender, EventArgs e)
        {
            var kh = new KeyboardHook(true);//this is from the new code the hook
            kh.KeyDown += Kh_KeyDown;//this is from the new code the hook that im trying to make it work
            textBox1.ReadOnly = true;
            IntPtr thisWindow = this.Handle;
            RegisterHotKey(thisWindow, 1, (uint)fsModifiers.Alt, (uint)Keys.Q);
            RegisterHotKey(thisWindow, 2, (uint)fsModifiers.Alt, (uint)Keys.W);
            RegisterHotKey(thisWindow, 3, (uint)fsModifiers.Alt, (uint)Keys.E);
        }
    
        private static void Kh_KeyDown(Keys key, bool Shift, bool Ctrl, bool Alt)
        {
             //how i use this information to do the actions in WNDProc down there
            Debug.WriteLine("The Key: " + key);
        }
    
    
        public enum fsModifiers
        {
            NoModifier = 0x000,
            Alt = 0x0001,
            Control = 0x0002,
            Shift = 0x0004,
            Window = 0x0008,
        }
    
        private void Menu_FormClosed(object sender, FormClosedEventArgs e)
        {
            UnregisterHotKey(thisWindow, 1);
            UnregisterHotKey(thisWindow, 2);
            UnregisterHotKey(thisWindow, 3);
        }
          //i want this to happen when not focused
        protected override void WndProc(ref Message keyPressed)
        {
            IntPtr i = keyPressed.WParam;
            DateTime lastPressedTime = DateTime.MinValue;
    
            if (i == (IntPtr)1 && keyPressed.Msg == 0x0312)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
    
                    ShowWindow(this.Handle, SW_RESTORE);
                    TopMost = true;
                }
                else
                {
                    this.WindowState = FormWindowState.Minimized;
                    TopMost = false;
                }
            } 
            base.WndProc(ref keyPressed);
        }
 
Typically when you want something to happen, you would write code for it. The brute force solution would be to duplicate the code. The better way would be to move the common functionality that you want happen into a method, and then have both your hotkey handler and your window procedure call that method.
 

Latest posts

Back
Top Bottom