zoukankan      html  css  js  c++  java
  • C# register global hotkey ,onekey 注册多个全局热键以及单个全局热键

    我们需要用非Hook的方法,来给我们的app 或者winform注册热键. 就像下面的 , 欧陆词典注册的一个热键F6一样,

    在winform最小化的情况下,也能够全局响应热键. 这里使用系统API来注册.能够达到最好的性能.

    代码如下:

    image

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace ExampleForm
    {
        public partial class Form1 : Form
        {
            //[System.Runtime.InteropServices.DllImport("user32.dll")]
            //private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

            enum KeyModifier
            {
                None = 0,
                Alt = 1,
                Control = 2,
                Shift = 4,
                WinKey = 8
            }
            public Form1()
            {
                InitializeComponent();
                int id = 0;     // The id of the hotkey.
                //RegisterHotKey(this.Handle, id, (int)KeyModifier.Shift, Keys.A.GetHashCode());       // 注册 Shift + A 为全局热键
                RegisterHotKey(this.Handle, id, 0, Keys.F3.GetHashCode());//注册单个按钮F3,只需要把int fsModifiers设置为0即可
            }
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);

                if (m.Msg == 0x0312)
                {
                   

                    Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);                  // The key of the hotkey that was pressed.
                    KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF);       // The modifier of the hotkey that was pressed.
                    int id = m.WParam.ToInt32();                                        // The id of the hotkey that was pressed.


                    MessageBox.Show("Hotkey has been pressed!");
                    // do something
                }
            }

            private void ExampleForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                UnregisterHotKey(this.Handle, 0);       // Unregister hotkey with id 0 before closing the form. You might want to call this more than once with different id values if you are planning to register more than one hotkey.
            }
            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void button1_Click(object sender, EventArgs e)
            {
            }
        }
    }

  • 相关阅读:
    #研发中间件介绍#定时任务调度与管理JobCenter
    分享一个分布式定时任务系统 ( python)
    APScheduler + Gearman 构建分布式定时任务调度-std1984-ITPUB博客
    分布式缓存的一起问题 – 后端技术 by Tim Yang
    新兵训练营系列课程——Feed架构介绍
    Mysql分库分表方案
    可扩展性设计之数据切分
    你的数据库数据量上亿,为了提高效率,要分库还是分表?具体怎么做
    58同城mysql分库分表实践-沈剑
    可动态扩展的分库分表策略浅谈
  • 原文地址:https://www.cnblogs.com/caianhua/p/6856129.html
Copyright © 2011-2022 走看看