private static string ConvertHotkey(HotkeySettings hotkey) { Key key = KeyInterop.KeyFromVirtualKey(hotkey.Code); HotkeyModel model = new HotkeyModel(hotkey.Alt, hotkey.Shift, hotkey.Win, hotkey.Ctrl, key); return(model.ToString()); }
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.InvokeAsync(async() => { await Task.Delay(500); SetHotkey(hotkeyModel); }); }
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 async Task 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("success"); } tbMsg.Visibility = Visibility.Visible; OnHotkeyChanged(); var token = hotkeyUpdateSource.Token; await Task.Delay(500, token); if (token.IsCancellationRequested) { return; } FocusManager.SetFocusedElement(FocusManager.GetFocusScope(this), null); Keyboard.ClearFocus(); } }
private void SetHotkey(HotkeyModel hotkeyModel, HotkeyCallback action) { string hotkeyStr = hotkeyModel.ToString(); try { Hotkey hotkey = new Hotkey { Alt = hotkeyModel.Alt, Shift = hotkeyModel.Shift, Ctrl = hotkeyModel.Ctrl, Win = hotkeyModel.Win, Key = (byte)KeyInterop.VirtualKeyFromKey(hotkeyModel.CharKey), }; _hotkeyHandle = HotkeyManager.RegisterHotkey(hotkey, action); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception) #pragma warning restore CA1031 // Do not catch general exception types { string errorMsg = string.Format(CultureInfo.InvariantCulture, InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr); MessageBox.Show(errorMsg); } }
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("success"); } tbMsg.Visibility = Visibility.Visible; OnHotkeyChanged(); } }
private 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); } }
private void HotkeyTextBox_PreviewKeyDown(object sender, KeyEventArgs e) { e.Handled = true; Key key = (e.Key == Key.System ? e.SystemKey : e.Key); var hkm = new HotkeyModel(GlobalHotkey.Instance.ModifyKeyStatus, key); var hotkeyStr = hkm.ToString(); if (hotkeyStr == HotkeyText) { return; } HotkeyText = hotkeyStr; }
private void SetHotkey(HotkeyModel hotkeyModel, HotkeyCallback action) { string hotkeyStr = hotkeyModel.ToString(); try { Hotkey hotkey = new Hotkey(); hotkey.Alt = hotkeyModel.Alt; hotkey.Shift = hotkeyModel.Shift; hotkey.Ctrl = hotkeyModel.Ctrl; hotkey.Win = hotkeyModel.Win; hotkey.Key = (byte)KeyInterop.VirtualKeyFromKey(hotkeyModel.CharKey); _hotkeyHandle = _hotkeyManager.RegisterHotkey(hotkey, action); } catch (Exception) { string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr); MessageBox.Show(errorMsg); } }
private void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e) { hotkeyUpdateSource?.Cancel(); hotkeyUpdateSource?.Dispose(); hotkeyUpdateSource = new(); var token = hotkeyUpdateSource.Token; e.Handled = true; //when alt is pressed, the real key should be e.SystemKey Key key = e.Key == Key.System ? e.SystemKey : e.Key; SpecialKeyState specialKeyState = GlobalHotkey.CheckModifiers(); var hotkeyModel = new HotkeyModel( specialKeyState.AltPressed, specialKeyState.ShiftPressed, specialKeyState.WinPressed, specialKeyState.CtrlPressed, key); var hotkeyString = hotkeyModel.ToString(); if (hotkeyString == tbHotkey.Text) { return; } _ = Dispatcher.InvokeAsync(async() => { await Task.Delay(500, token); if (!token.IsCancellationRequested) { await SetHotkey(hotkeyModel); } }); }
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); } }