C7HotKey
class C7HotKey
{
Ref the API for shortKey#region Ref the API for shortKey
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
#endregion
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
private Form form;
public C7HotKey(Form form)
{
this.form = form;
this.form.FormClosing += new FormClosingEventHandler(FormClosing);
}
private void FormClosing(object sender, FormClosingEventArgs e)
{
UnRegisterHotKey();
}
public delegate void MethodWithHotKey();
private Dictionary<int, MethodWithHotKey> hotKeyIdToMethod = new Dictionary<int, MethodWithHotKey>();
private Dictionary<KeyValuePair<KeyModifiers, Keys>, int> keysToId = new Dictionary<KeyValuePair<KeyModifiers, Keys>, int>();
/**//// <summary>
/// Registers the hot key.
/// </summary>
/// <param name="keyModifier">The key modifier.</param>
/// <param name="key">The key.</param>
/// <param name="theMethod">The method.</param>
public void RegisterHotKey(KeyModifiers keyModifier, Keys key, MethodWithHotKey theMethod)
{
int newId = hotKeyIdToMethod.Count + 1;
while (hotKeyIdToMethod.ContainsKey(newId))
{
newId++;
}
if (keysToId.ContainsKey(new KeyValuePair<KeyModifiers, Keys>(keyModifier, key)))
{
UnRegisterHotKey(keyModifier, key);
}
bool isRegOK = RegisterHotKey(this.form.Handle, newId, keyModifier, key);
if (!isRegOK)
{
throw new Exception("Can't register the hotkey, please retry ! ");
}
hotKeyIdToMethod.Add(newId, theMethod);
keysToId.Add(new KeyValuePair<KeyModifiers, Keys>(keyModifier, key), newId);
}
/**//// <summary>
/// Unregister hot key.
/// </summary>
public void UnRegisterHotKey()
{
if (hotKeyIdToMethod == null || hotKeyIdToMethod.Count == 0)
{
return;
}
foreach (KeyValuePair<int, MethodWithHotKey> thisPair in hotKeyIdToMethod)
{
UnregisterHotKey(this.form.Handle, thisPair.Key);
}
}
/**//// <summary>
/// Unregister hot key.
/// </summary>
/// <param name="modifier">The modifier.</param>
/// <param name="key">The key.</param>
public void UnRegisterHotKey(KeyModifiers modifier, Keys key)
{
KeyValuePair<KeyModifiers, Keys> keysPair = new KeyValuePair<KeyModifiers, Keys>(modifier, key);
if (keysToId.ContainsKey(keysPair))
{
if (UnregisterHotKey(this.form.Handle, keysToId[keysPair]))
{
keysToId.Remove(keysPair);
}
}
}
/**//// <summary>
/// Runs the method. used to be invoked by WndProc(ref Message m)
/// </summary>
/// <param name="msg">The MSG.</param>
public void RunMethod(Message msg)
{
const int WM_HOTKEY = 0x312;
switch (msg.Msg)
{
case (WM_HOTKEY):
{
foreach (KeyValuePair<int, MethodWithHotKey> thisPair in hotKeyIdToMethod)
{
if (msg.WParam.ToInt32() == thisPair.Key)
{
if (thisPair.Value != null)
{
thisPair.Value.Invoke();
}
}
}
break;
}
}
}
}
}
class C7HotKey
{
Ref the API for shortKey#region Ref the API for shortKey
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
#endregion
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
private Form form;
public C7HotKey(Form form)
{
this.form = form;
this.form.FormClosing += new FormClosingEventHandler(FormClosing);
}
private void FormClosing(object sender, FormClosingEventArgs e)
{
UnRegisterHotKey();
}
public delegate void MethodWithHotKey();
private Dictionary<int, MethodWithHotKey> hotKeyIdToMethod = new Dictionary<int, MethodWithHotKey>();
private Dictionary<KeyValuePair<KeyModifiers, Keys>, int> keysToId = new Dictionary<KeyValuePair<KeyModifiers, Keys>, int>();
/**//// <summary>
/// Registers the hot key.
/// </summary>
/// <param name="keyModifier">The key modifier.</param>
/// <param name="key">The key.</param>
/// <param name="theMethod">The method.</param>
public void RegisterHotKey(KeyModifiers keyModifier, Keys key, MethodWithHotKey theMethod)
{
int newId = hotKeyIdToMethod.Count + 1;
while (hotKeyIdToMethod.ContainsKey(newId))
{
newId++;
}
if (keysToId.ContainsKey(new KeyValuePair<KeyModifiers, Keys>(keyModifier, key)))
{
UnRegisterHotKey(keyModifier, key);
}
bool isRegOK = RegisterHotKey(this.form.Handle, newId, keyModifier, key);
if (!isRegOK)
{
throw new Exception("Can't register the hotkey, please retry ! ");
}
hotKeyIdToMethod.Add(newId, theMethod);
keysToId.Add(new KeyValuePair<KeyModifiers, Keys>(keyModifier, key), newId);
}
/**//// <summary>
/// Unregister hot key.
/// </summary>
public void UnRegisterHotKey()
{
if (hotKeyIdToMethod == null || hotKeyIdToMethod.Count == 0)
{
return;
}
foreach (KeyValuePair<int, MethodWithHotKey> thisPair in hotKeyIdToMethod)
{
UnregisterHotKey(this.form.Handle, thisPair.Key);
}
}
/**//// <summary>
/// Unregister hot key.
/// </summary>
/// <param name="modifier">The modifier.</param>
/// <param name="key">The key.</param>
public void UnRegisterHotKey(KeyModifiers modifier, Keys key)
{
KeyValuePair<KeyModifiers, Keys> keysPair = new KeyValuePair<KeyModifiers, Keys>(modifier, key);
if (keysToId.ContainsKey(keysPair))
{
if (UnregisterHotKey(this.form.Handle, keysToId[keysPair]))
{
keysToId.Remove(keysPair);
}
}
}
/**//// <summary>
/// Runs the method. used to be invoked by WndProc(ref Message m)
/// </summary>
/// <param name="msg">The MSG.</param>
public void RunMethod(Message msg)
{
const int WM_HOTKEY = 0x312;
switch (msg.Msg)
{
case (WM_HOTKEY):
{
foreach (KeyValuePair<int, MethodWithHotKey> thisPair in hotKeyIdToMethod)
{
if (msg.WParam.ToInt32() == thisPair.Key)
{
if (thisPair.Value != null)
{
thisPair.Value.Invoke();
}
}
}
break;
}
}
}
}
}
在Form_Load或者其他事件中调用RegisterHotKey方法就可以了。但是现在的问题是还是必须在Form中执行:
protected override void WndProc(ref Message m)
{
if (this.hotkey != null)
{
hotkey.RunMethod(m);
}
base.WndProc(ref m);
}
不知道怎么能在Form中只是Register,其他的都不用管呢?