C#设置全局热键

Hotkey.cs:
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public delegate void HotkeyEventHandler(int hotkeyid);

    public class Hotkey : IMessageFilter
    {
        Hashtable keyIDs = new Hashtable();
        IntPtr hWnd;

        public event HotkeyEventHandler OnHotkey;

        public enum KeyFlags
        {
            MOD_ALT = 0x1,
            MOD_CONTROL = 0x2,
            MOD_SHIFT = 0x4,
            MOD_WIN = 0x8
        }

        [DllImport("user32.dll")]
        public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);

        [DllImport("user32.dll")]
        public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);

        [DllImport("kernel32.dll")]
        public static extern UInt32 GlobalAddAtom(String lpString);

        [DllImport("kernel32.dll")]
        public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);

        public Hotkey(IntPtr hWnd)
        {
            this.hWnd = hWnd;
            Application.AddMessageFilter(this);
        }

        public int RegisterHotkey(Keys key, KeyFlags keyflags)
        {
            UInt32 hotkeyid = GlobalAddAtom(Guid.NewGuid().ToString());
            RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)key);
            keyIDs.Add(hotkeyid, hotkeyid);
            return (int)hotkeyid;
        }

        public void UnregisterHotkeys()
        {
            Application.RemoveMessageFilter(this);
            foreach (UInt32 key in keyIDs.Values)
            {
                UnregisterHotKey(hWnd, key);
                GlobalDeleteAtom(key);
            }
        }

        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == 0x312)
            {
                if (OnHotkey != null)
                {
                    foreach (UInt32 key in keyIDs.Values)
                    {
                        if ((UInt32)m.WParam == key)
                        {
                            OnHotkey((int)m.WParam);
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }
}

Form1.cs:
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Hotkey _hotkey;
        private int _hotkeyid;

        public Form1()
        {
            InitializeComponent();
        }

        public void OnHotkey(int hotkeyid)
        {
            if (hotkeyid == _hotkeyid)
            {
                if (this.Visible == true)
                    this.Visible = false;
                else
                    this.Visible = true;
            }
            else
            {
                this.Visible = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _hotkey = new Hotkey(this.Handle);
            _hotkeyid = _hotkey.RegisterHotkey(Keys.F2, Hotkey.KeyFlags.MOD_CONTROL);   //定义快键(Ctrl + F2)
            _hotkey.OnHotkey += new HotkeyEventHandler(OnHotkey);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _hotkey.UnregisterHotkeys();
        }
    }
}


上一篇: GIF文件格式
下一篇: C#键盘按键监视
文章来自: cnblogs.com
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 2 | 引用: 0 | 查看次数: 5477
dnawo[2009-09-07 06:16 PM | | | 120.36.4.53 | del | 回复回复]
板凳
代码都在上边了,测试过的,Copy就能用。
ztitan[2009-09-06 12:34 AM | | | 119.141.170.80 | del | 回复回复]
沙发
你好,请问你能发这个程序的源文件给我吗?我邮箱是199989277@qq.com,打扰你拉~~
发表评论
登录后再发表评论!