Search code examples
c#hotkeys

How to set a hotkey to a Non-Form Program


Okay so I'm making this simple screenshot program using bitmaps and such, but when i try to make a hotkey like f12, for instance, nothing happens, i coded it just to show a message box, but it doesn't even do that. So i set it back to do both message box and take a screenshot but still doesn't work. private static NotifyIcon notifyIcon;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {

        System.Windows.Forms.Application.EnableVisualStyles();
        System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
        // We need to dispose here, or the icon will not remove until the 
        // system tray is updated.
        System.Windows.Forms.Application.ApplicationExit += delegate
    {
        notifyIcon.Dispose();
    };
        CreateNotifyIcon();
        System.Windows.Forms.Application.Run();
    }

    /// <summary>
    /// Creates the icon that sits in the system tray.
    /// </summary>
    ///


    static void Program_KeyDown(object sender, KeyEventArgs e)
    {
      if(e.KeyCode == Keys.F12)
        {
            MessageBox.Show("ScreenShot Taken");
            TakeFullScreenShot();
        }
      else if(e.KeyCode == Keys.F11)
        {
            Application.Exit();
        }
    }
    private static void CreateNotifyIcon()
    {
        notifyIcon = new NotifyIcon
        {
            Icon = Resources.AppIcon, ContextMenu = GetContextMenu()
        };
            notifyIcon.Visible = true;

    }

    private static ContextMenu GetContextMenu()
    {

        string myPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
        System.Diagnostics.Process prc = new System.Diagnostics.Process();
        prc.StartInfo.FileName = myPath;
        ContextMenu menu = new ContextMenu();
        menu.MenuItems.Add("Take Screenshot (F12)", delegate { TakeFullScreenShot(); });
        menu.MenuItems.Add("Open Folder", delegate { prc.Start(); });
        menu.MenuItems.Add("Exit", delegate { System.Windows.Forms.Application.Exit(); });

        return menu;

    }

    private static void TakeFullScreenShot()
    {
        int width = Screen.PrimaryScreen.Bounds.Width;
        int height = Screen.PrimaryScreen.Bounds.Height;

        using (Bitmap screenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb))
        {
            using (Graphics graphics = Graphics.FromImage(screenshot))
            {
                Point origin = new Point(0, 0);
                Size screenSize = Screen.PrimaryScreen.Bounds.Size;
                //Copy Entire screen to entire bitmap.
                graphics.CopyFromScreen(origin, origin, screenSize);
            }

            //Check to see if the file exists, if it does, append.
            int append = 1;

            while (File.Exists($"Screenshot{append}.jpg"))
                append++;

            string fileName = $"Screenshot{append}.jpg";
            screenshot.Save(fileName, ImageFormat.Jpeg);
        }
    }

You may not need all of that but its just to make sure i didn't mess anything up when trying to build it. Also, this program has no form to it, its just a icon in your taskbar, if you right click on it and click Take Screenshot(F12) It will take the screenshot with out problem. Thank you!


Solution

  • The hotkey will not work unless the program has focus. The Key events are only sent to your application when it has focus.