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. 读书笔记—CLR via C#章节11-13

    前言 这本书这几年零零散散读过两三遍了,作为经典书籍,应该重复读反复读,既然我现在开始写博了,我也准备把以前觉得经典的好书重读细读一遍,并且将笔记整理到博客中,好记性不如烂笔头,同时也在写的过程中也可 ...

  2. Tomcat源码学习一

    这段时间工作不太忙,所以抽时间学习了TOMCAT, TOMCAT实际就是负责保持TCP连接传递到部署的项目中.浏览器实质就是TCP发送器.将用户的请求封装成TCP发送请求.当然格式是双方协定的.使用的 ...

  3. Bootstrap面包屑导航

    Bootstrap中提供了面包屑导航的实现方法: 只需要引入bootstrap.css文件即可. 主要引用的样式有: .span6 .breadcrumb 实例代码如下: <!DOCTYPE h ...

  4. Java、C#双语版HttpHelper类

    Java.C#双语版HttpHelper类(解决网页抓取乱码问题)   在做一些需要抓取网页的项目时,经常性的遇到乱码问题.最省事的做法是去需要抓取的网站看看具体是什么编码,然后采用正确的编码进行解码 ...

  5. 一个快速找第k+1小的算法

    public static int randomSelect(int[] A, int k)         {             return randomSelectDo(A, 0, A.L ...

  6. Binder机制,从Java到C (4. Parcel)

    1. 远程调用的数据传输 在远程的函數中,必然会需要传递一些数据,那这些数据是怎么传输的呢? 在IPC中,Proxy端的作用就是将一些参数打包,然后发送出去,下面是在Proxy端经常会遇见的调用远程方 ...

  7. Hadoop集成

    Hadoop集成   长期以来,我每开个系列,只有兴趣写一篇,很难持之与恒.为了克服这个长久以来的性格弱点,以及梳理工作半年的积累.最近一个月会写两篇关于Mongo在地理大数据方面的实践和应用,一篇关 ...

  8. Asp.net MVC4 CodeFirst 使用EFTracingProvider

    一.关于EFTracingProvider EFTracingProvider相关信息见作者博客:关于EFTracingProvider EFTracingProvider Demo下载地址:Trac ...

  9. session验证登陆- 页面跳转

    用session验证登陆,当用户想访问一个页面时由于没有登录,就跳转到登录页面,登录后跳转到用户请求的页面,在session跳转中传上次请求的页面.  怎么获得这个url值并跳转到该页面呢? 以此跳转 ...

  10. 顺手的Linux发行版及其工具推荐

    从Windows切换到Linux已经有半年多的时间了,简单给大家推荐一些个人感觉不错的软件,主要都是和开发相关的通用软件--- 0.archlinux  挑一个比较顺手的linux发行版当然是首要任务 ...