• c# 注册全局热键


    //引入系统API
            [DllImport("user32.dll")]
            static extern bool RegisterHotKey(IntPtr hWnd, int id, int modifiers, Keys vk);
            [DllImport("user32.dll")]
            static extern bool UnregisterHotKey(IntPtr hWnd, int id);
    
    
            int keyid = 10;     //区分不同的快捷键
            Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>();   //每一个key对于一个处理函数
            public delegate void HotKeyCallBackHanlder();
    
            //组合控制键
            public enum HotkeyModifiers
            {
                Alt = 1,
                Control = 2,
                Shift = 4,
                Win = 8
            }
    
            //注册快捷键
            public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack)
            {
                int id = keyid++;
                if (!RegisterHotKey(hWnd, id, modifiers, vk))
                    throw new Exception("注册失败!");
                keymap[id] = callBack;
            }
    
            // 注销快捷键
            public void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack)
            {
                foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap)
                {
                    if (var.Value == callBack)
                    {
                        UnregisterHotKey(hWnd, var.Key);
                        return;
                    }
                }
            }
    
            // 快捷键消息处理
            public void ProcessHotKey(Message m)
            {
                if (m.Msg == 0x312)
                {
                    int id = m.WParam.ToInt32();
                    HotKeyCallBackHanlder callback;
                    if (keymap.TryGetValue(id, out callback))
                        callback();
                }
            }
    public Form1()
            {
                InitializeComponent();
            }
    
            HotKeys h = new HotKeys();
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (button1.Text == "注册")
                {
                    string vaule = textBox1.Text;
                    Regist(vaule);
                    button1.Text = "卸载";
                    label2.Text = "注册成功"; 
                }
                else
                {
                    h.UnRegist(this.Handle, CallBack);
                    button1.Text = "注册";
                    label2.Text = "卸载成功";
                }
            }
    
            private void Regist(string str)
            {
                if (str == "")
                    return;
                int modifiers = 0;
                Keys vk = Keys.None;
                foreach (string value in str.Split('+'))
                {
                    if (value.Trim() == "Ctrl")
                        modifiers = modifiers + (int)HotKeys.HotkeyModifiers.Control;
                    else if (value.Trim() == "Alt")
                        modifiers = modifiers + (int)HotKeys.HotkeyModifiers.Alt;
                    else if (value.Trim() == "Shift")
                        modifiers = modifiers + (int)HotKeys.HotkeyModifiers.Shift;
                    else
                    {
                        if (Regex.IsMatch(value, @"[0-9]"))
                        {
                            vk = (Keys)Enum.Parse(typeof(Keys), "D" + value.Trim());
                        }
                        else
                        {
                            vk = (Keys)Enum.Parse(typeof(Keys), value.Trim());
                        }
                    }
                }
                //这里注册了Ctrl+Alt+E 快捷键
                h.Regist(this.Handle, modifiers, vk, CallBack);
            }
    
            //按下快捷键时被调用的方法
            public void CallBack()
            {
                label2.Text = "快捷键被调用!";
            }
    
            protected override void WndProc(ref Message m)
            {
                //窗口消息处理函数
                h.ProcessHotKey(m);
                base.WndProc(ref m);
            }
    
            private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                StringBuilder keyValue = new StringBuilder();
                keyValue.Length = 0;
                keyValue.Append("");
                if (e.Modifiers != 0)
                {
                    if (e.Control)
                        keyValue.Append("Ctrl + ");
                    if (e.Alt)
                        keyValue.Append("Alt + ");
                    if (e.Shift)
                        keyValue.Append("Shift + ");
                }
                if ((e.KeyValue >= 33 && e.KeyValue <= 40) ||
                    (e.KeyValue >= 65 && e.KeyValue <= 90) ||   //a-z/A-Z
                    (e.KeyValue >= 112 && e.KeyValue <= 123))   //F1-F12
                {
                    keyValue.Append(e.KeyCode);
                }
                else if ((e.KeyValue >= 48 && e.KeyValue <= 57))    //0-9
                {
                    keyValue.Append(e.KeyCode.ToString().Substring(1));
                }
                ((TextBox)sender).Text = keyValue.ToString();
            }
    
            private void textBox1_KeyUp(object sender, KeyEventArgs e)
            {
                string str = ((TextBox)sender).Text.TrimEnd();
                int len = str.Length;
                if (len >= 1 && str.Substring(str.Length - 1) == "+")
                {
                    ((TextBox)sender).Text = "";
                }
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                h.UnRegist(this.Handle, CallBack);
            }

    界面

  • 相关阅读:
    Codeforces Round #192 (Div. 2)
    STL删除vector或list的方法及注意的问题
    HDU 4614 (13年多校第二场1004)裸线段树
    将汇编的指令画在图片上==可以贴图法操作==动动图片图案指示就行了。(堆积木编译器)
    MySQL Connector/Python 安装、测试
    crtmpserver的架构简介
    hi3531的i2c部分 分类: HI3531 2014-03-18 14:41 948人阅读 评论(0) 收藏
    bootrom的构成 分类: vxWorks 2014-03-14 08:48 315人阅读 评论(0) 收藏
    bootrom脚本的创建 分类: vxWorks 2014-03-14 08:47 307人阅读 评论(0) 收藏
    ROM型启动方式概述 分类: vxWorks 2014-03-14 08:47 297人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/margin-gu/p/5887853.html
Copyright © 2020-2023  润新知