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. 【SSRS】入门篇(六) -- 分组和总计

    原文:[SSRS]入门篇(六) -- 分组和总计 通过[SSRS]入门篇(五) -- 设置报表格式的设计,一份简单格式的报表已产生,如下图: 这节来说说分组和总计: 根据日期.订单对数据进行分组 添加 ...

  2. [翻译]初识SQL Server 2005 Reporting Services Part 4

    原文:[翻译]初识SQL Server 2005 Reporting Services Part 4 这一篇是关于SQL Server 2005 Reporting Services四篇文章中最后一篇 ...

  3. LINQ TO SQL ——Group by

    原文:LINQ TO SQL --Group by 分组在SQL中应用的十分普遍,在查询,统计时都有可能会用到它.LINQ TO SQL中同样具备group的功能,这篇我来讲下LINQ TO SQL中 ...

  4. SQL面试题1

    SQL面试题 Sql常用语法 下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言 ...

  5. 利用XCode来进行IOS的程序开发

    利用XCode来进行IOS的程序开发 本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换 ...

  6. CSDN 高校俱乐部: 排列搜索

    CSDN 高校俱乐部/英雄会 题目: 设数组a包含n个元素恰好是0..n - 1的一个排列,给定b[0],b[1],b[2],b[3]问有多少个0..n-1的排列a,满足(a[a[b[0]]]*b[0 ...

  7. Django是Python下的一款网络服务器框架

    被解放的姜戈01 初试天涯   Django是Python下的一款网络服务器框架.Python下有许多款不同的框架.Django是重量级选手中最有代表性的一位.许多成功的网站和APP都基于Django ...

  8. HTTP压缩算法SDCH

    程序设计中使用的那些共享方法或者技术 前段时间看了个paper是讲述谷歌浏览器使用的压缩方法SDCH,其实原理还是比较简单的. 看了论文后就想总结一下程序中使用的一些共享方法或者技术吧. 1.Goog ...

  9. Mysql中实现多表关联查询更新操作

    今天一下要记录一下才行了,每次都要去网上查找方法,每次都难找得要命 Mysql在更新某些字段的数据时,有时候会依据其他表的数据进行更新,需要通过关联后对不同的行更新不同的值,传统的update set ...

  10. sql 通过存储过程和自定义类型批量新增数据

    1,建立存储过程 create PROCEDURE [dbo].[p_Company_Insert] @CompanyCollection [CompanyTableType] READONLY AS ...