I remember that when I was still using VB6 sereval years ago, if global hotkeys are required, a massive system-hooking program would be used.

That's terrible,wasn't it? :)

But in C#, this can be much easier.We can use system api : RegisterHotKey and UnregisterHotKey(This links to the official document of Microsoft)

What to do next? Certianly is to encapsulate those function into a class in order to use it rapidly.(Thanks for Sir.bomo for his template,this is his page)

In the following class , the function ProcessKey is of great importance.

In the main form-class, a \(void\)  \(WndProc(ref\ Message\ m)\) is required,which used to processing the hotkey sends in.

Mention:The class \(keys\) is in \(System.Windows.Forms\), so surprising.

Code:(After you register the hotkey,press Ctrl+Alt+S to hide/show you window.)

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Diagnostics; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
HotKeys h = new HotKeys(); public Form1()
{
InitializeComponent();
} private void btnRegist_Click(object sender, EventArgs e)
{
if (h.Regist(this.Handle, (int)HotKeys.HotkeyModifiers.Shift + (int)HotKeys.HotkeyModifiers.Control, Keys.S, CallBack))
MessageBox.Show("Success");
else MessageBox.Show("Failed");
}
private void btnUnregist_Click(object sender, EventArgs e)
{
if (h.UnRegist(this.Handle, CallBack))
MessageBox.Show("Success");
else MessageBox.Show("Failed");
} protected override void WndProc(ref Message m)
{
h.ProcessHotKey(m); //Processing
base.WndProc(ref m);
} public void CallBack()
{
this.Visible = !this.Visible;
} private void Form1_Load(object sender, EventArgs e)
{ } } public class HotKeys
{ [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); private int keyid = ;
private Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>();
public delegate void HotKeyCallBackHanlder();
private const int WM_HOTKEY = 0x312; public enum HotkeyModifiers
{
Alt = ,
Control = ,
Shift = ,
Win =
} public bool Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack) //Regist Hotkey
{
try
{
int id = keyid++;
if (!RegisterHotKey(hWnd, id, modifiers, vk)) return false;
keymap[id] = callBack;
}
catch { return false; } //make the class stronger
return true;
} public bool UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack) //UnRegister hotkey
{
bool bx = false;
foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap)
{
if (var.Value == callBack)
{
try
{
UnregisterHotKey(hWnd, var.Key);
keymap.Remove(var.Key); //I think this line is important,which the original author not add.
}
catch { }
bx = true;
}
}
return bx; } public void ProcessHotKey(Message m) //Processing and React
{
if (m.Msg == WM_HOTKEY) //The original author puts 0x312 here.WM_HOTKEY can be clearer.
{
int id = m.WParam.ToInt32();
HotKeyCallBackHanlder callback;
if (keymap.TryGetValue(id, out callback))
callback();
}
}
}
}

【C#】【SHARE】The registering of global hotkeys的更多相关文章

  1. 【docker-compose】使用docker-compose部署运行spring boot+mysql 【处理容器的时区问题】【详解】【福利:使用docker-compose构建 wordpress+mysql】

    ==================================================================================================== ...

  2. 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

    [题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  3. 【程序员小助手】Synergy,感受穿越屏幕之美

    内容简介 1.Synergy简介 2.Synergy安装与配置 3.附录 [程序员小助手]系列 在这个系列文章中(不定期更新),小编会把这些年(也没几年)的编程学习和工作中使用到的个人感觉非常好的软件 ...

  4. 【程序员小助手】Emacs,最强编辑器,没有之一

    内容简介 1.Emacs简介 2.Emacs三个平台的安装与配置 3.自动补全插件 4.小编的Emacs配置文件 5.常用快捷方式 6.和版本控制系统的配合(以SVN为例) [程序员小助手]系列 在这 ...

  5. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  6. 微信开发】【Asp.net MVC】-- 微信分享功能

    [微信开发][Asp.net MVC]-- 微信分享功能 2017-01-15 09:09 by stoneniqiu, 12886 阅读, 15 评论, 收藏, 编辑 内嵌在微信中的网页,右上角都会 ...

  7. 【Python】【容器 | 迭代对象 | 迭代器 | 生成器 | 生成器表达式 | 协程 | 期物 | 任务】

    Python 的 asyncio 类似于 C++ 的 Boost.Asio. 所谓「异步 IO」,就是你发起一个 IO 操作,却不用等它结束,你可以继续做其他事情,当它结束时,你会得到通知. Asyn ...

  8. 1.spring boot起步之Hello World【从零开始学Spring Boot】

    [视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...

  9. 0. 前言【从零开始学Spring Boot】

    [视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...

随机推荐

  1. 如何优雅的研究 RGSS3 (七) 加入LOGO屏幕

    对于游戏 LOGO 屏幕. 首先设计 LOGO Scene类.我们知道,现场类 Scene_Base 子类. 让我们回顾一下现场的作品. 首先运行开始处理.其次是开始治疗.然后停止更新屏幕,最后,治疗 ...

  2. VS2013中Python学习环境搭建

    VS2013中Python学习笔记[环境搭建] 前言 Python是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python的设计具有很强的可读性,相比其他语言经常使用英文关键字, ...

  3. Date与SimpleDateFormat

    Date常用的方法 返回类型 方法名称 备注 Date New Date() 创建当前日期对象 Date New date(long dt) 使用自1970.1.1以后的指定毫秒数创建日期 boole ...

  4. 关于ADO.NET参数化查询的提问

    最近我们的正式环境一直在报错一个异常,首先我贴出来异常信息 BLL层 捕获到了请求的url Net.BLL.MobileFun MobileFun.GetBusinessBidPolicy 异常 传入 ...

  5. SOLID 设计原则 In C# 代码实现

    [S] Single Responsibility Principle (单一职责原则) 认为一个对象应该仅只有一个单一的职责 namespace SingleResponsibilityPrinci ...

  6. EditPlus 3设置字体大小

    EditPlus设置字体大小 tools ---> preferences ---> fonts

  7. [转]loadView的用法,loadView创建基本界面,DidLoad读入数据

    loadview: //   有没有nib 只要是复写了loadview loadview都会被执行     有nib文件的话加载的是nib文件的view  没有的话会按照loadview里的代码加载 ...

  8. Competitive

  9. jquery 分页控件2

    jquery 分页控件(二) 上一章主要是关于分页控件的原理,代码也没有重构.在这一章会附上小插件的下载链接,插件主要就是重构逻辑部分,具体可以下载源文件看下,源代码也有注释.为了测试这个插件是能用的 ...

  10. 在Netbeans上配置Android开发环境

    在NetBeans下开发Android的所需要的基本条件:NetBeans(包含JDK)+Android SDK+NBAndroid(为Netbeans设计的Android 开发插件) 详情:http ...