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 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 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(); } }
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)); }
private void LoadSetting() { bool isCtrl = false; bool isShift = false; bool isAlt = false; bool isWin = false; bool ReplaceWindowR = false; Key key = Key.Space; SettingObject.Instance.Get("Hotkey", "IsCtrl", out isCtrl, true); SettingObject.Instance.Get("Hotkey", "IsShift", out isShift, false); SettingObject.Instance.Get("Hotkey", "IsAlt", out isAlt, true); SettingObject.Instance.Get("Hotkey", "IsWin", out isWin, false); SettingObject.Instance.Get("Hotkey", "Key", out key, Key.Space); SettingObject.Instance.Get("Hotkey", "ReplaceWindowR", out ReplaceWindowR, true); var model = new HotkeyModel(isCtrl, isShift, isAlt, isWin, key); GlobalHotkey.Instance.RegistHotkey("HotKey.ShowApp", model); GlobalHotkey.Instance.ReplaceWindowR = ReplaceWindowR; string version = null; SettingObject.Instance.Get("Version", "version", out version, "0.6"); VersionManager.Instance.Version = version; }
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); }); }
public void SetHotkey(string hotkeyStr, EventHandler <HotkeyEventArgs> action) { var hotkey = new HotkeyModel(hotkeyStr); try { HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action); } catch (Exception) { MessageBox.Show("Register hotkey: " + hotkeyStr + " failed."); } }
private void RegisterHotkey(string identifier, HotkeyModel hotkey, EventHandler <HotkeyEventArgs> handler) { try { HotkeyManager.Current.AddOrReplace(identifier, hotkey.Key, hotkey.Modifiers, handler); } catch (HotkeyAlreadyRegisteredException) { var msg = $"Failed to register Pinpoint hotkey, {_settingsWindow.Model.HotkeyToggleVisibility.Text} seems to already be bound. You can pick another one in settings."; MessageBox.Show(msg, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
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; }
public void OnHotkeyCaptured(object sender, GlobalHotkey.HotkeyCapturedEventArgs e) { if (e.Available) { _globalHotkey.RemoveHotkey(Hotkey); Hotkey = e.Hotkey; var message = _translater.GetTranslation("succeed"); App.API.ShowMsg(message, iconPath: Constant.AppIcon); } else { var message = _translater.GetTranslation("hotkeyUnavailable"); App.API.ShowMsg(message, iconPath: Constant.ErrorIcon); } }
private bool CheckHotAvailabel(HotkeyModel hotkey) { try { HotkeyManager.Current.AddOrReplace("HotkeyAvailableTest", hotkey.CharKey, hotkey.ModifierKeys, OnHotkey); return(true); } catch { } finally { HotkeyManager.Current.Remove("HotkeyAvailableTest"); } return(false); }
internal static bool CheckAvailability(HotkeyModel currentHotkey) { try { HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", currentHotkey.CharKey, currentHotkey.ModifierKeys, (sender, e) => { }); return(true); } catch { } finally { HotkeyManager.Current.Remove("HotkeyAvailabilityTest"); } return(false); }
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 InitializeHotkey() { var hotkey = new HotkeyModel(Settings.Hotkey); HotkeyViewModel = new HotkeyViewModel { Hotkey = hotkey }; foreach (var key in Settings.CustomHotkeys) { var m = new HotkeyModel(key.Hotkey); var cvm = new CustomHotkeyViewModel { HotkeyViewModel = { Hotkey = m }, CustomHotkey = { Hotkey = m, Query = key.Query } }; CustomHotkeyViewModels.Add(cvm); } }
public void SetHotkey(string keyStr, bool triggerValidate = true) { tbMsg.Visibility = Visibility.Visible; tbHotkey.Text = keyStr; tbHotkey.Select(tbHotkey.Text.Length, 0); CurrentHotkey = new HotkeyModel(keyStr); if (triggerValidate) { CurrentHotkeyAvailable = CheckHotAvailabel(CurrentHotkey); if (!CurrentHotkeyAvailable) { tbMsg.Foreground = new SolidColorBrush(Colors.Red); tbMsg.Text = "hotkey unavailable"; } else { tbMsg.Foreground = new SolidColorBrush(Colors.Green); tbMsg.Text = "succeed"; } OnOnHotkeyChanged(); } }
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); } }); }
private void SetHotkey(string hotkeyStr, EventHandler <HotkeyEventArgs> action) { var hotkey = new HotkeyModel(hotkeyStr); SetHotkey(hotkey, action); }
private void SetHotkey(string hotkeyStr, HotkeyCallback action) { var hotkey = new HotkeyModel(hotkeyStr); SetHotkey(hotkey, action); }
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); } }
public void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action) { var hotkey = new HotkeyModel(hotkeyStr); SetHotkey(hotkey, action); }