Search code examples
c#wpfkeypresstopmostminimized

why can't I use keybinds outside the program?


I'm try make program to open other apps, to open this program i wan't use keybinds and for hide too.

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (Dark == 0)
            {
                if (e.Key == Key.LeftCtrl)
                {
                    if (e.Key == Key.LWin)
                    {
                        this.Topmost = true;
                        Dark = 1;
                    }
                }
            }
            else if(Dark == 1)
            {
                if (e.Key == Key.LeftCtrl)
                {
                    if (e.Key == Key.LWin)
                    {
                        this.Topmost = false;
                        Dark = 0;
                        WindowState = WindowState.Minimized;
                    }
                }
                else if (e.Key == Key.Escape)
                {
                    this.Topmost = false;
                    Dark = 0;
                    WindowState = WindowState.Minimized;
                }
            }

Outside the program, keybinds do not work at all, but if the program is open, then they work, why?


Solution

  • you can refer to my class, used to global hook key event https://github.com/nhochjkaru/JEOrbwalk/blob/master/UserActivityHook.cs

    declare: UserActivityHook actHook; Main function:

    actHook = new UserActivityHook(); 
    actHook.KeyDown += new KeyEventHandler(Window_KeyDown);