本來聽說winapi裡面的RegisterHotKey有時候不好使,於是打算用hook鍵盤消 息來做hook key,結果發現hook那裡處理消息那裡還挺麻煩,也想會不會造成系 統的額外開銷比較大,所以最後用RegisterHotKey來封裝了一下,測試沒有問題 ,還是可以的。(winform)
下面的代碼包括一個Textbox的處理,和一個 封裝了api的類。Textbox那裡就是處理用戶定義的熱鍵的界面表示,比如用戶按 鍵的時候會出現"Ctrl+Alt+A"之類的,Api封裝那裡做了處理,只管 Register好了,不用管UnRegister,當注冊一個一樣的Id的hotkey,會自動先 UnRegister原來的,然後最後關閉窗口的時候,調用一下HotkeyHelper.Dispose ()就可以了。還希望多加建議。here we go..
----------------------- ---------------------------------------------------------
Textbox 部分:(Key hotkeyVk = null; )
private void txbHotKey_KeyDown(object sender, KeyEventArgs e)
{
txbHotKey.Text = string.Empty;
if (e.Control)
{
txbHotKey.Text += "Ctrl+";
}
if (e.Alt)
{
txbHotKey.Text += "Alt+";
}
if (e.Shift)
{
txbHotKey.Text += "Shift+";
}
if (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z)
{
if (e.Modifiers != Keys.None)
{
txbHotKey.Text += e.KeyCode.ToString();
hotkeyVk = e.KeyCode;
}
}
else if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)
{
if (e.Modifiers != Keys.None)
{
txbHotKey.Text += e.KeyCode.ToString().Remove(0, 1);
hotkeyVk = e.KeyCode;
}
}
else if (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
{
if (e.Modifiers != Keys.None)
{
txbHotKey.Text += e.KeyCode.ToString ().Replace("Pad", "");
hotkeyVk = e.KeyCode;
}
}
else if (e.KeyCode >= Keys.F1 && e.KeyCode <= Keys.F12)
{
txbHotKey.Text += e.KeyCode.ToString();
hotkeyVk = e.KeyCode;
}
e.SuppressKeyPress = false;
e.Handled = true;
}
private void txbHotKey_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
private void txbHotKey_KeyUp (object sender, KeyEventArgs e)
{
CheckHotkey();
}
void txbHotKey_LostFocus(object sender, EventArgs e)
{
CheckHotkey();
}
private void CheckHotkey()
{
if (txbHotKey.Text.EndsWith("+") || txbHotKey.Text == string.Empty)
{
txbHotKey.Text = "無";
}
}
private void btnHotKey_Click(object sender, EventArgs e)
{
string hotkey = txbHotKey.Text.Replace("+", "");
if (hotkey != "無" && hotkey != string.Empty)
{
uint modifies = 0;
if (hotkey.Contains ("Ctrl"))
{
modifies |= HotkeyHelper.MOD_CONTROL;
hotkey = hotkey.Replace ("Ctrl", "");
}
if (hotkey.Contains ("Alt"))
{
modifies |= HotkeyHelper.MOD_ALT;
hotkey = hotkey.Replace("Alt", "");
}
if (hotkey.Contains("Shift"))
{
modifies |= HotkeyHelper.MOD_SHIFT;
hotkey = hotkey.Replace("Shift", "");
}
GameProvider.HotkeyHelper.RegisterHotKey(this.Handle, hotkeyId, modifies, (uint)hotkeyVk);
}
}
HotkeyHelper部分 :
using System;
using System.Collections.Generic;
using System.Text;
using GFSucker.Game.Utility;
namespace GFSucker.Game.Provider
{
public class HotkeyHelper : IDisposable
{
public const int MOD_ALT = 0x1;
public const int MOD_CONTROL = 0x2;
public const int MOD_SHIFT = 0x4;
public const int MOD_WIN = 0x8;
public const int WM_HOTKEY = 0x312;
private Dictionary dicHotkeys;
public HotkeyHelper()
{
dicHotkeys = new Dictionary();
}
public bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk)
{
if (dicHotkeys.ContainsKey(id))
{
if (dicHotkeys[id].IsRegistered)
{
UnregisterHotKey (dicHotkeys[id].HWnd, id); // unregister firstly
dicHotkeys [id].IsRegistered = false;
}
dicHotkeys[id].HWnd = hWnd; // set the new hwnd (normally should be the same)
}
else
{
dicHotkeys.Add(id, new Hotkey(hWnd, false));
}
dicHotkeys[id].IsRegistered = WinApi.RegisterHotKey(hWnd, id, fsModifiers, vk);
return dicHotkeys[id].IsRegistered;
}
private bool UnregisterHotKey(IntPtr hWnd, int id)
{
return WinApi.UnregisterHotKey(hWnd, id);
}
#region Hotkey Information
class Hotkey
{
public Hotkey(IntPtr hWnd, bool isRegistered)
{
_HWnd = hWnd;
_IsRegistered = isRegistered;
}
private IntPtr _HWnd;
public IntPtr HWnd
{
get { return _HWnd; }
set { _HWnd = value; }
}
private bool _IsRegistered;
public bool IsRegistered
{
get { return _IsRegistered; }
set { _IsRegistered = value; }
}
}
#endregion
#region IDisposable 成員
public void Dispose()
{
foreach (int id in dicHotkeys.Keys)
{
if (dicHotkeys[id].IsRegistered)
{
UnregisterHotKey(dicHotkeys[id].HWnd, id);
}
}
}
#endregion
}
}
Winapi聲明:
[DllImport("user32.dll")]
[return: MarshalAs (UnmanagedType.Bool)]
public static extern bool RegisterHotKey (IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport ("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
Override winform窗體的WndProc:(就是接受到hotkey信息的時候,做 你想做的東西)
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == HotkeyHelper.WM_HOTKEY)
{
MainForm mainform = this.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent as MainForm;
if (mainform != null)
{
if (mainform.WindowState == FormWindowState.Minimized)
{
mainform.WindowState = FormWindowState.Normal;
}
mainform.Activate();
}
}
}
------- ---------------------------------------------------------------------- ---
因為直接從程序中copy出來的,代碼沒有很多注釋,不過都是非常淺 顯易懂的東西,名字看上去還是一目了然的,另外說一句,我覺得如果重構得比 較好的代碼是不需要做過多的注釋的,代碼本身就是注釋,注釋用在解釋一些比 較混亂的行為意圖。希望對大家有幫助咯。