如何设置非表单程序的热键

时间:2017-04-20 10:11:21

标签: c# hotkeys

好的,所以我正在使用位图等制作这个简单的截图程序,但是当我尝试制作像f12这样的热键时,没有任何反应,我编码它只是为了显示一个消息框,但它没有&甚至这样做。所以我把它设置回来做两个消息框并截取屏幕截图,但仍然无法正常工作。         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);
        }
    }

你可能不需要所有这些,但它只是为了确保在尝试构建它时我没有弄乱任何东西。此外,这个程序没有任何形式,它只是你的任务栏中的一个图标,如果你右键单击它并点击截图(F12)它将截取没有问题的屏幕截图。谢谢!

1 个答案:

答案 0 :(得分:0)

除非程序具有焦点,否则热键将无效。 Key事件只有在有焦点时才会发送到您的应用程序。