解决方案 »

  1.   

    热键需要注册使用的:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        class Hotkey
        {
            /// <summary>
            /// 快捷键消息
            /// </summary>
            public const int WM_HOTKEY = 0x0312;        [DllImport("user32.dll", SetLastError = true)]
            public static extern bool RegisterHotKey( IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk  );        [DllImport("user32.dll", SetLastError = true)]
            public static extern bool UnregisterHotKey(  IntPtr hWnd, int id  );        [DllImport("kernel32.dll", EntryPoint = "GlobalAddAtomW")]
            private static extern int GlobalAddAtomW(string name);        [Flags()]
            public enum KeyModifiers
            {
                None = 0,
                Alt = 1,
                Control = 2,
                Shift = 4,
                Windows = 8
            }        public static int GlobalAddAtom(string name)
            {
                return GlobalAddAtomW(name) - 0xC000;
            }
        }
    }
    bool hotok1 = false;
    int hotid1 = Hotkey.GlobalAddAtom("myhotkey1");private void Form1_Load(object sender, EventArgs e)
    {
        hotok1 = Hotkey.RegisterHotKey(this.Handle, hotid1, Hotkey.KeyModifiers.None, Keys.F2);
    }private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (hotok1)
        {
            Hotkey.UnregisterHotKey(this.Handle, hotid1);
        }
    }protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == Hotkey.WM_HOTKEY)
        {
            int id = m.WParam.ToInt32();
            if (id == hotid1)
            {
                MessageBox.Show("热键消息来了");
            }
        }
    }
      

  2.   

    http://www.codeproject.com/Articles/442285/Global-Shortcuts-in-WinForms-and-WPF
      

  3.   

    1 楼的代码可以满足你的需求。只是自定义按键你需要自己写判断键盘的按键。
    3 楼提供的demo ,完美解决了...
    学习了...