/// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="modifiers"></param> /// <param name="description"></param> /// <returns>If added successfully, returns the <see cref="Hotkey"/> that was created, otherwise null.</returns> public virtual Hotkey Register(Keys key, ModKeys modifiers, string description) { if (_registers.Any(hkk => string.Equals(hkk.Description, description) || (hkk.Modifiers == modifiers && hkk.Key == key))) { return(null); } Hotkey hk = Hotkey.Create(key, modifiers, description); _registers.Add(hk); if (IsHooked == false) { try { IsHooked = Hook(); } catch (Exception e) { throw new Exception("Failed to perform hook. see inner exception.", e); } } return(hk); }
/// <summary> /// Supports event from AKey press. /// </summary> /// <param name="description">Description of the specific key, must be unique.</param> public static HotkeyEventArgs Create(AKey k, string description) { return(new HotkeyEventArgs { Hotkey = Hotkey.Create(k, description) }); }
private void process_keyevent(KeyEventProcessArgs e) { int wParam = e.wParam; var lParam = e.lParam; var keyc = ((KeyCode)lParam.vkCode); var key = keyc.ToKeys(); var modifier = ModKeys.None; if ((!SuppressKeyHold || duplicatetags[(int)keyc] != (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))) { #region Modifier Switches if (keyc.IsModifier()) { switch (keyc) { case KeyCode.RControl: rctrl = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; //is key down? otherwise false. break; case KeyCode.LControl: lctrl = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; //is key down? otherwise false. break; case KeyCode.RAlt: ralt = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; //is key down? otherwise false. break; case KeyCode.LAlt: lalt = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; //is key down? otherwise false. break; case KeyCode.RShift: rshift = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; //is key down? otherwise false. break; case KeyCode.LShift: lshift = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; //is key down? otherwise false. break; } } else { if (rctrl) { modifier |= ModKeys.RControl; } if (lctrl) { modifier |= ModKeys.LControl; } if (rshift) { modifier |= ModKeys.RControl; } if (lshift) { modifier |= ModKeys.LShift; } if (ralt) { modifier |= ModKeys.RAlt; } if (lalt) { modifier |= ModKeys.LAlt; } } #endregion #region Args and Event firing //Console.WriteLine("(" + ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) ? "D" : "U") + ") " + modifier.ToString() + " + " + key.ToString()); Hotkey hotk; HotkeyEventArgs args = null; if ((hotk = base._registers.FirstOrDefault(hk => hk.Key.Equals(key) && hk.Modifiers.Compare(modifier))) != null) { //keydown if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) { args = InvokeKeyDown(this, hotk); } else //key up { if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP)) { args = InvokeKeyUp(this, hotk); //if equals to last key event is up and equals to this key event -> KeyPress // hotkey mode: /*tmp_key.CompareModifiers(key) && tmp_mods.CompareModifiers(modifier) && !(wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)*/ if (args == null || (args.Handled == false && presstags[(int)keyc])) { args = InvokeKeyPress(this, hotk); } } } } /*if (args != null && args.Handled) * return 1;*/ #endregion presstags[(int)keyc] = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; #if DEBUG_KEYS Console.Clear(); Console.WriteLine("RCTRL " + rctrl + " | " + "LCTRL " + lctrl + " | " + "RAlt " + ralt + " | " + "LAlt " + lalt + " | "); Console.WriteLine("RShift " + rshift + " | " + "LShift " + lshift); Console.WriteLine(Hotkey.Create(key, modifier, "").ToString()); #endif } if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && duplicatetags[(int)keyc] == false) { duplicatetags[(int)keyc] = true; } else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && duplicatetags[(int)keyc] == true) { duplicatetags[(int)keyc] = false; } }
private void process_keyevent(KeyEventProcessArgs e) { int wParam = e.wParam; var lParam = e.lParam; var keyc = ((KeyCode)lParam.vkCode); var key = keyc.ToKeys(); var modifier = ModKeys.None; if ((!SuppressKeyHold || duplicatetags[(int)keyc] != (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))) { #region Modifier Switches if (keyc.IsModifier()) { switch (keyc) { case KeyCode.RControl: rctrl = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; //is key down? otherwise false. break; case KeyCode.LControl: lctrl = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; //is key down? otherwise false. break; case KeyCode.RAlt: ralt = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; //is key down? otherwise false. break; case KeyCode.LAlt: lalt = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; //is key down? otherwise false. break; case KeyCode.RShift: rshift = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; //is key down? otherwise false. break; case KeyCode.LShift: lshift = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; //is key down? otherwise false. break; } } else { //todo sometimes when pressing modifier really quick, it may screw up, try using Control.Modifiers to determine the mods asap. // problem is that it may be called half a sec later from the real press. and its so quick that only press down may be invoked here. if (rctrl) { modifier |= ModKeys.RControl; } if (lctrl) { modifier |= ModKeys.LControl; } if (rshift) { modifier |= ModKeys.RControl; } if (lshift) { modifier |= ModKeys.LShift; } if (ralt) { modifier |= ModKeys.RAlt; } if (lalt) { modifier |= ModKeys.LAlt; } } #endregion #region Args and Event firing //Console.WriteLine("(" + ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) ? "D" : "U") + ") " + modifier.ToString() + " + " + key.ToString()); Hotkey hotk = new Hotkey(key, modifier, null); HotkeyEventArgs args = null; //keydown if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) { args = InvokeKeyDown(this, hotk); } else //key up { if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP)) { args = InvokeKeyUp(this, hotk); //if equals to last key event is up and equals to this key event -> KeyPress // hotkey mode: /*tmp_key.CompareModifiers(key) && tmp_mods.CompareModifiers(modifier) && !(wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)*/ if (args == null || (args.Handled == false && presstags[(int)keyc])) { args = InvokeKeyPress(this, hotk); } } } /*if (args != null && args.Handled) * return 1;*/ #endregion presstags[(int)keyc] = wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN; #if DEBUG_KEYS Console.Clear(); Console.WriteLine("RCTRL " + rctrl + " | " + "LCTRL " + lctrl + " | " + "RAlt " + ralt + " | " + "LAlt " + lalt + " | "); Console.WriteLine("RShift " + rshift + " | " + "LShift " + lshift); Console.WriteLine(Hotkey.Create(key, modifier, "").ToString()); #endif } if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && duplicatetags[(int)keyc] == false) { duplicatetags[(int)keyc] = true; } else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && duplicatetags[(int)keyc] == true) { duplicatetags[(int)keyc] = false; } }