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. NCache实现Oracle数据与分布式缓存数据同步的3个步骤

    多层次结构的应用程序是目前发展的趋势,这种程序都需要庞大的数据库支持.而数据传输的能力直接影响程序性能,成为程序可扩展性的瓶颈.因此很多开发者开始在程序中使用内存分布式缓存来提高程序性能. 同时,内存 ...

  2. Juqery遮罩插件

    Juqery遮罩插件,想罩哪就罩哪!   一  前言 在项目开发时发现没有一个用起来 爽一点的遮罩插件,看起来觉得不难 好吧那就利用空闲时间,自己折腾一个吧,也好把jquery再温习一下, 需要的功能 ...

  3. 实例学习SSIS(四)--使用日志记录和错误流重定向

    原文:实例学习SSIS(四)--使用日志记录和错误流重定向 导读: 实例学习SSIS(一)--制作一个简单的ETL包 实例学习SSIS(二)--使用迭代 实例学习SSIS(三)--使用包配置 实例学习 ...

  4. leetcode[87] Partition List

    题目:给定一个链表和一个数x,将链表中比x小的放在前面,其他的放在后头.例如: Given 1->4->3->2->5->2 and x = 3,return 1-> ...

  5. Android 5.0自定义动画

    材料设计中的动画对用户的操作给予了反馈,并且在与应用交互时提供了持续的可见性.材料主题提供了一些按钮动画和活动过渡,Android 5.0允许你自定义动画并且可以创建新的动画: Touch Feedb ...

  6. java使用maven创建springmvc web项目

    创建maven项目,使用maven-archetype-webapp 创建完成后首先是在pom.xml里增加maven的依赖 <dependencies> <dependency&g ...

  7. 安装dynamics CRM 2013提示“实例名称必须与计算机名称相同”

    在安装CRM 2013的时候,最后一步一直提示“实例名称必须与计算机名称相同”. 原因是在安装数据库之后,我更改了计算机名称.因此就导致了可这个错. 在安装数据库的时候,数据库会记住计算机的名称,用 ...

  8. 链表c语言实现

    链表(c语言实现)--------------小练习   #include <stdio.h> #include <stdlib.h> #include <string. ...

  9. 2014.first[未填]

    之后就按照自己的直觉,整理了第一套,难度为简单,差不多比2013noipday1水一点...先练练手而已 T1 vijos1196吃糖果游戏 博弈论 依题意,我们可知,如果去分数目为2,3,7,8必输 ...

  10. dtrace sample

    #!/usr/sbin/dtrace -s #pragma D option flowindent /* monitor file open */ syscall::open:entry { prin ...