public void SetHotkey(HotkeyModel keyModel, bool triggerValidate = true) { CurrentHotkey = keyModel; tbHotkey.Text = CurrentHotkey.ToString(); tbHotkey.Select(tbHotkey.Text.Length, 0); if (triggerValidate) { CurrentHotkeyAvailable = CheckHotkeyAvailability(); if (!CurrentHotkeyAvailable) { tbMsg.Foreground = new SolidColorBrush(Colors.Red); tbMsg.Text = InternationalizationManager.Instance.GetTranslation("hotkeyUnavailable"); } else { tbMsg.Foreground = new SolidColorBrush(Colors.Green); tbMsg.Text = InternationalizationManager.Instance.GetTranslation("succeed"); } tbMsg.Visibility = Visibility.Visible; OnHotkeyChanged(); } }
public void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action) { string hotkeyStr = hotkey.ToString(); try { HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action); } catch (Exception) { string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr); MessageBox.Show(errorMsg); } }
void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e) { e.Handled = true; tbMsg.Visibility = Visibility.Hidden; //when alt is pressed, the real key should be e.SystemKey Key key = (e.Key == Key.System ? e.SystemKey : e.Key); SpecialKeyState specialKeyState = GlobalHotkey.Instance.CheckModifiers(); var hotkeyModel = new HotkeyModel( specialKeyState.AltPressed, specialKeyState.ShiftPressed, specialKeyState.WinPressed, specialKeyState.CtrlPressed, key); var hotkeyString = hotkeyModel.ToString(); if (hotkeyString == tbHotkey.Text) { return; } Dispatcher.DelayInvoke("HotkeyAvailabilityTest", o => { SetHotkey(hotkeyModel); }, TimeSpan.FromMilliseconds(500)); }
public void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action) { var hotkey = new HotkeyModel(hotkeyStr); SetHotkey(hotkey, action); }