public void RegisterHotkey(IntPtr hWnd, MK modifier, VK_Keys key, PerformHotkeyAction action, string actionParameter) { HotkeyStore hks = new HotkeyStore() { ActionParameter = actionParameter, HWnd = hWnd, Action = action, Controller = (VK_Keys)(int)key, Modifier = modifier, UniqueIdentifier = incrementalHotkeyRegister }; if (!hks.Register()) { int err = Marshal.GetLastWin32Error(); string secondary = "Error:" + err.ToString(); if (err == 1409) { secondary = "The hotkey has already been registered. Use a different combination than (" + modifier.ToString() + "+" + key.ToString(); } throw new InvalidProgramException("The call to register a hotkey with windows failed. " + secondary); } interestedKeyCombos.Add(hks); incrementalHotkeyRegister++; }
// public to allow for unit testing, its mildly complex set of rules. public void ParseKeyStringToModAndKey(string keyString, out MK mod, out VK_Keys key) { #region entry code if (string.IsNullOrEmpty(keyString)) { throw new ArgumentNullException("keyString", "The keyString must be non empty"); } if (keyString.IndexOf('_') < SHORTESTIDLENGTH) { throw new ArgumentOutOfRangeException("keyString", "The keystring must be specified in the form MODIFIER_KEY"); } #endregion key = (VK_Keys)0; string[] parts = keyString.Split(new char[] { '_' }); if (parts.Length != 2) { throw new ArgumentOutOfRangeException("The keyString was not passed in the format MODIFIER_KEY"); } mod = ParseModifierPartOfString(parts[0]); key = ParseKeyPartOfString(parts[1]); if (mod == (MK)0) { throw new ArgumentOutOfRangeException("The modifier passed was not correctly formed"); } }
public bool ProcessWindowsMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam) { if (interestedKeyCombos.Count == 0) { return(false); } if (msg == (int)NativeMethods.WindowMessages.HOTKEY) { VK_Keys key = (VK_Keys)(((int)lParam >> 16) & 0xFFFF); MK modifier = (MK)((int)lParam & 0xFFFF); foreach (var hitTest in interestedKeyCombos) { if ((hitTest.Modifier == modifier) && (hitTest.Controller == key)) { hitTest.Action(hitTest.ActionParameter); return(true); } } } return(false); }
private VK_Keys ParseKeyPartOfString(string keyString) { keyString = keyString.ToUpperInvariant(); VK_Keys result = (VK_Keys)0; foreach (var v in Enum.GetNames(typeof(VK_Keys))) { if (v.Equals(keyString, StringComparison.OrdinalIgnoreCase)) { result = (VK_Keys)Enum.Parse(typeof(VK_Keys), keyString, true); break; } } if (result == (VK_Keys)0) { #region switch statement that checks all of the keys that have friendly names switch (keyString) { case "NUM+": result = VK_Keys.Oemplus; break; case "NUM0": result = VK_Keys.NumPad0; break; case "NUM1": result = VK_Keys.NumPad1; break; case "NUM2": result = VK_Keys.NumPad2; break; case "NUM3": result = VK_Keys.NumPad3; break; case "NUM4": result = VK_Keys.NumPad4; break; case "NUM5": result = VK_Keys.NumPad5; break; case "NUM6": result = VK_Keys.NumPad6; break; case "NUM7": result = VK_Keys.NumPad7; break; case "NUM8": result = VK_Keys.NumPad8; break; case "NUM9": result = VK_Keys.NumPad9; break; case "1": result = VK_Keys.D1; break; case "2": result = VK_Keys.D2; break; case "3": result = VK_Keys.D3; break; case "4": result = VK_Keys.D4; break; case "5": result = VK_Keys.D5; break; case "6": result = VK_Keys.D6; break; case "7": result = VK_Keys.D7; break; case "8": result = VK_Keys.D8; break; case "9": result = VK_Keys.D9; break; case "0": result = VK_Keys.D0; break; default: throw new ArgumentOutOfRangeException("keyString", "The key value was not of the expected type. Not all keys are registered. (" + keyString + ")"); } #endregion } return(result); }
public static extern void keybd_event(VK_Keys bVk, byte bScan, KeyEvent dwFlags, uint dwExtraInfo);