/// <summary> /// Instatiates a new Hotkey which can be registered with the <c>HotKeyManager</c> class. See <see cref="HotkeyManager.AddHotkey(Hotkey)"/> /// </summary> /// <param name="key">A virtual key from <see cref="VirtualKeyCodes.VirtualKeys"/></param> /// <param name="modifiers">Modifierkeys needed to trigger hotkey. Bitwise or from <see cref="KeyModifiers"/></param> /// <param name="action">The callback which is executed when the hotkey is pressed</param> public Hotkey(VirtualKeyCodes.VirtualKeys key, KeyModifiers modifiers, Action action) { HotkeyId = GetHashCode(); Key = KeyInterop.KeyFromVirtualKey((int)key); VirtualKey = (int)key; Modifiers = modifiers; Action = action; }
/// <summary> /// Helper method for creating a keyup keybdinput /// </summary> /// <param name="key"></param> /// <returns></returns> private static WinApi.KEYBDINPUT CreateKeyUp(VirtualKeyCodes.VirtualKeys key) { return(new WinApi.KEYBDINPUT() { dwExtraInfo = IntPtr.Zero, dwFlags = (int)(WinApi.DWFLAGS.KEYEVENTF_KEYUP | WinApi.DWFLAGS.KEYEVENTF_SCANCODE), wScan = (ushort)WinApi.MapVirtualKey((uint)key, 0), wVk = (ushort)key }); }
/// <summary> /// Releases a held key /// </summary> /// <param name="key">The key to be released</param> public static void SendKeyUp(VirtualKeyCodes.VirtualKeys key) { WinApi.INPUT input = new WinApi.INPUT(); input.input = new WinApi.COMBINEDINPUT() { keyboardInput = CreateKeyUp(key) }; input.type = 1; WinApi.SendInput(1, new WinApi.INPUT[] { input }, Marshal.SizeOf(typeof(WinApi.INPUT))); }
/// <summary> /// Emulates a single keypress /// </summary> /// <param name="key">A <see cref="VirtualKeyCodes.VirtualKeys"/> key</param> public static void SendVirtualKeyCode(VirtualKeyCodes.VirtualKeys key) { SendVirtualKeyCode(new VirtualKeyCodes.VirtualKeys[] { key }); }