码迷,mamicode.com
首页 > Windows程序 > 详细

【C#】【SHARE】The registering of global hotkeys

时间:2016-09-28 10:17:50      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

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(reg Message m) \) is required,which used to processing the hotkey sends in.

 

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

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Runtime;
 11 using System.Runtime.InteropServices;
 12 using System.Diagnostics;
 13 
 14 namespace WindowsFormsApplication1
 15 {
 16     public partial class Form1 : Form
 17     {
 18         HotKeys h = new HotKeys();
 19 
 20         public Form1()
 21         {
 22             InitializeComponent();
 23         }
 24 
 25         private void btnRegist_Click(object sender, EventArgs e)
 26         {
 27             if (h.Regist(this.Handle, (int)HotKeys.HotkeyModifiers.Shift + (int)HotKeys.HotkeyModifiers.Control, Keys.S, CallBack))
 28                 MessageBox.Show("Success");
 29             else MessageBox.Show("Failed");
 30         }
 31         private void btnUnregist_Click(object sender, EventArgs e)
 32         {
 33             if (h.UnRegist(this.Handle, CallBack))
 34                 MessageBox.Show("Success");
 35             else MessageBox.Show("Failed");
 36         }
 37 
 38         protected override void WndProc(ref Message m)
 39         {
 40             h.ProcessHotKey(m);  //Processing
 41             base.WndProc(ref m);
 42         }
 43 
 44         public void CallBack()
 45         {
 46             this.Visible = !this.Visible;
 47         }
 48 
 49         private void Form1_Load(object sender, EventArgs e)
 50         {
 51 
 52         }
 53 
 54 
 55         
 56     }
 57 
 58 
 59     public class HotKeys
 60     {
 61 
 62         [DllImport("user32.dll")]
 63         static extern bool RegisterHotKey(IntPtr hWnd, int id, int modifiers, Keys vk);
 64         [DllImport("user32.dll")]
 65         static extern bool UnregisterHotKey(IntPtr hWnd, int id);
 66         
 67 
 68 
 69         private int keyid = 10;     
 70         private Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>();   
 71         public delegate void HotKeyCallBackHanlder();
 72         private const int WM_HOTKEY = 0x312;
 73 
 74         public enum HotkeyModifiers
 75         {
 76             Alt = 1,
 77             Control = 2,
 78             Shift = 4,
 79             Win = 8
 80         }
 81 
 82         public bool Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack)  //Regist Hotkey
 83         {
 84             try
 85             {
 86                 int id = keyid++;
 87                 if (!RegisterHotKey(hWnd, id, modifiers, vk)) return false;
 88                 keymap[id] = callBack;
 89             }
 90             catch { return false; }  //make the class stronger
 91             return true;
 92         }
 93 
 94         public bool UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack)  //UnRegister hotkey
 95         {
 96             bool bx = false;
 97             foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap)
 98             {
 99                 if (var.Value == callBack)
100                 {
101                     try
102                     {
103                         UnregisterHotKey(hWnd, var.Key);
104                         keymap.Remove(var.Key);  //I think this line is important,which the original author not add.
105                     }
106                     catch { }
107                     bx = true;
108                 }
109             }
110             return bx;
111             
112         }
113 
114         public void ProcessHotKey(Message m)   //Processing and React
115         {
116             if (m.Msg == WM_HOTKEY) //The original author puts 0x312 here.WM_HOTKEY can be clearer.
117             {
118                 int id = m.WParam.ToInt32();
119                 HotKeyCallBackHanlder callback;
120                 if (keymap.TryGetValue(id, out callback))
121                     callback();
122             }
123         }
124     }
125 }

 

【C#】【SHARE】The registering of global hotkeys

标签:

原文地址:http://www.cnblogs.com/Fefnir/p/5915036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!