个人代码库の全局快捷键

 

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;

static class Hotkey
{
/*注:快捷键是通过消息触发的,因此要重载WndProc函数,
* 在里面添加对快捷键回调消息的处理方法Hotkey.ProcessHotKey(m)。
* 注意,回调函数(要执行的方法,动作等)不加括号!
// protected override void WndProc(ref Message m)
// {
// base.WndProc(ref m);
// Hotkey.ProcessHotKey(m);
// }
*/
#region 系统api
[DllImport(
"user32.dll")]
[
return: MarshalAs(UnmanagedType.Bool)]
static extern bool RegisterHotKey(IntPtr hWnd, int id, HotkeyModifiers fsModifiers, Keys vk);

[DllImport(
"user32.dll")]
static extern bool UnregisterHotKey(IntPtr hWnd, int id);
#endregion

/// <summary>
/// 注册快捷键
/// </summary>
/// <param name="hWnd">持有快捷键窗口的句柄</param>
/// <param name="fsModifiers">组合键</param>
/// <param name="vk">快捷键的虚拟键码</param>
/// <param name="callBack">回调函数</param>
public static void Regist(IntPtr hWnd, HotkeyModifiers fsModifiers, Keys vk, HotKeyCallBackHanlder callBack)
{
int id = keyid++;
if (!RegisterHotKey(hWnd, id, fsModifiers, vk))
throw new Exception("regist hotkey fail.");
keymap[id]
= callBack;
}

/// <summary>
/// 注销快捷键
/// </summary>
/// <param name="hWnd">持有快捷键窗口的句柄</param>
/// <param name="callBack">回调函数</param>
public static void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack)
{
foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap)
{
if (var.Value == callBack)
UnregisterHotKey(hWnd, var.Key);
}
}

/// <summary>
/// 快捷键消息处理
/// </summary>
public static void ProcessHotKey(System.Windows.Forms.Message m)
{
if (m.Msg == WM_HOTKEY)
{
int id = m.WParam.ToInt32();
HotKeyCallBackHanlder callback;
if (keymap.TryGetValue(id, out callback))
{
callback();
}
}
}

const int WM_HOTKEY = 0x312;
static int keyid = 10;
static Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>();
public delegate void HotKeyCallBackHanlder();
}

enum HotkeyModifiers
{
MOD_ALT
= 0x1,
MOD_CONTROL
= 0x2,
MOD_SHIFT
= 0x4,
MOD_WIN
= 0x8
}
/*附:虚拟键值表附:常用模拟键的键值对照表。

                      键盘键与虚拟键码对照表

      字母和数字键     数字小键盘的键       功能键         其它键
      键   键码     键   键码       键   键码     键      键码
      A   65       0   96        F1   112     Backspace    8
      B   66       1   97        F2   113     Tab       9
      C   67       2   98        F3   114     Clear      12
      D   68       3   99        F4   115     Enter      13
      E   69       4   100       F5   116     Shift      16
      F   70       5   101       F6   117     Control     17
      G   71       6   102       F7   118      Alt       18
      H   72       7   103       F8   119     Caps Lock    20
      I   73       8   104       F9   120     Esc       27
      J   74       9   105       F10  121     Spacebar    32
      K   75       *   106       F11  122     Page Up     33
      L   76       +   107       F12  123     Page Down    34
      M   77       Enter 108       --   --     End       35
      N   78       -   109       --   --      Home      36
      O   79       .   110       --   --      Left Arrow   37
      P   80       /   111       --   --      Up Arrow    38
      Q   81       --   --       --   --      Right Arrow   39
      R   82       --   --       --   --      Down Arrow    40
      S   83       --   --       --   --      Insert      45
      T   84       --   --       --   --      Delete      46
      U   85       --   --       --   --      Help       47
      V   86       --   --       --   --      Num Lock     144
      W   87          
      X   88      
      Y   89      
      Z   90      
      0   48      
      1   49      
      2   50       
      3   51       
      4   52       
      5   53       
      6   54       
      7   55       
      8   56       
      9   57  
*/

 

 

 

posted @ 2010-05-09 17:08  Asion Tang  阅读(344)  评论(0编辑  收藏  举报