![]() |
Sylloge
A C# helper library
|
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 using System; 00018 using System.Collections.Generic; 00019 00020 namespace Sylloge.Device 00021 { 00025 public class HotKeys : System.IDisposable 00026 { 00027 #region Local Constants 00028 00029 public enum KeyModifiers 00030 { 00031 None = 0, 00032 Alt = 1, 00033 //RAlt = 2, 00034 //LAlt = 4, 00035 Ctrl = 8, 00036 //RCtrl = 16, 00037 //LCtrl = 32, 00038 Shift = 64, 00039 //RShift = 128, 00040 //LShift = 256 00041 } 00042 00043 public enum KeyState 00044 { 00045 KeyUp, 00046 KeyDown 00047 } 00048 00049 public class KeyCombo 00050 { 00051 public Sylloge.WinAPI.KeybdInput Data { get; set; } 00052 public KeyModifiers Modifier { get; set; } 00053 public System.Windows.Forms.Keys Key { get; set; } 00054 public KeyState KeyState { get; set; } 00055 public string Tag { get; set; } 00056 00057 public KeyCombo() { } 00058 00059 public KeyCombo(KeyModifiers modifier, System.Windows.Forms.Keys key, KeyState state, string tag) 00060 { 00061 this.Modifier = modifier; 00062 this.Key = key; 00063 this.KeyState = state; 00064 this.Tag = tag; 00065 } 00066 00067 public KeyCombo(Sylloge.WinAPI.KeybdInput data, KeyModifiers modifier, System.Windows.Forms.Keys key, KeyState state, string tag) 00068 { 00069 this.Data = data; 00070 this.Modifier = modifier; 00071 this.Key = key; 00072 this.KeyState = state; 00073 this.Tag = tag; 00074 } 00075 } 00076 00077 #endregion 00078 00079 #region Events 00080 00081 public delegate void DelegateKeyComboError(string message); 00082 public delegate void DelegateKeyComboPress(KeyCombo keyCombo); 00083 public delegate void DelegateKeyPressed(KeyCombo keyCombo); 00087 public event DelegateKeyComboError KeyComboError; 00091 public event DelegateKeyComboPress KeyComboPress; 00095 public event DelegateKeyPressed KeyPressed; 00096 00097 private void OnKeyError(string message) 00098 { 00099 // This event is caught by who ever instantiates this class 00100 if (KeyComboError != null) { KeyComboError(message); } 00101 } 00102 00103 private void OnKeyComboPress(KeyCombo keyCombo, KeyState keyState) 00104 { 00105 keyCombo.KeyState = keyState; 00106 if (KeyComboPress != null) { KeyComboPress(keyCombo); } 00107 } 00108 00109 private void OnKeyPress(KeyCombo keyCombo) 00110 { 00111 if (KeyPressed != null) { KeyPressed(keyCombo); } 00112 } 00113 00114 #endregion 00115 00116 #region Local Attributes 00117 00118 private List<KeyCombo> _KeyCombos = new List<KeyCombo>(); 00119 00120 #endregion 00121 00122 #region Class Methods and Properties 00123 00127 public HotKeys() 00128 { 00129 // Create the handle for the global hot key hook 00130 if (!Sylloge.Device.GlobalKeyHandler.IsInitialized) { throw new Sylloge.Device.GlobalKeyHandler.UninitializedException(); } 00131 Sylloge.Device.GlobalKeyHandler.KeyPressed += new Sylloge.Device.GlobalKeyHandler.DelegateKeyPressed(this.GlobalKeyHandler_KeyPressed); 00132 this.Enabled = true; 00133 } 00134 00135 ~HotKeys() 00136 { 00137 this.Dispose(); 00138 } 00139 00140 public bool Enabled { get; set; } 00141 00142 public void Dispose() 00143 { 00144 this.Enabled = false; 00145 Sylloge.Device.GlobalKeyHandler.KeyPressed -= new Sylloge.Device.GlobalKeyHandler.DelegateKeyPressed(this.GlobalKeyHandler_KeyPressed); 00146 } 00147 00154 public void Add(KeyModifiers modifier, System.Windows.Forms.Keys key, string tag) 00155 { 00156 KeyCombo KeyCombo = new KeyCombo(); 00157 KeyCombo.Modifier = modifier; 00158 KeyCombo.Key = key; 00159 KeyCombo.Tag = tag; 00160 this._KeyCombos.Add(KeyCombo); 00161 } 00162 00163 private void GlobalKeyHandler_KeyPressed(int wParam, Sylloge.WinAPI.KeybdInput keyData) 00164 { 00165 if (!this.Enabled) { return; } 00166 HotKeys.KeyModifiers Modifier = KeyModifiers.None; 00167 if (wParam == Sylloge.WinAPI.Constants.WM_KEYDOWN || wParam == Sylloge.WinAPI.Constants.WM_SYSKEYDOWN || 00168 wParam == Sylloge.WinAPI.Constants.WM_KEYUP || wParam == Sylloge.WinAPI.Constants.WM_SYSKEYUP) { 00169 bool Ctrl = Convert.ToBoolean(Sylloge.WinAPI.User32.GetAsyncKeyState((int)System.Windows.Forms.Keys.ControlKey)); 00170 bool Shift = Convert.ToBoolean(Sylloge.WinAPI.User32.GetAsyncKeyState((int)System.Windows.Forms.Keys.ShiftKey)); 00171 bool Alt = Convert.ToBoolean(Sylloge.WinAPI.User32.GetAsyncKeyState((int)System.Windows.Forms.Keys.Menu)); 00172 if (Ctrl || Alt || Shift) { 00173 if (Ctrl) { Modifier |= KeyModifiers.Ctrl; } 00174 if (Shift) { Modifier |= KeyModifiers.Shift; } 00175 if (Alt) { Modifier |= KeyModifiers.Alt; } 00176 } 00177 KeyState State = KeyState.KeyUp; 00178 if (wParam == Sylloge.WinAPI.Constants.WM_KEYDOWN || wParam == Sylloge.WinAPI.Constants.WM_SYSKEYDOWN) { State = KeyState.KeyDown; } 00179 this.OnKeyPress(new KeyCombo(keyData, Modifier, ((System.Windows.Forms.Keys)keyData.wVk), State, string.Empty)); 00180 foreach (KeyCombo KeyCombo in this._KeyCombos) { 00181 if (Modifier == KeyCombo.Modifier && keyData.wVk == ((int)KeyCombo.Key)) { 00182 KeyCombo.Data = keyData; 00183 this.OnKeyComboPress(KeyCombo, State); 00184 break; 00185 } 00186 } 00187 } 00188 } 00189 00190 #endregion 00191 } 00192 }
1.7.4