コード例 #1
0
        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());
        }
コード例 #2
0
        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);
            });
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        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();
            }
        }
コード例 #5
0
        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);
            }
        }
コード例 #6
0
        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();
            }
        }
コード例 #7
0
        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);
            }
        }
コード例 #8
0
        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;
        }
コード例 #9
0
        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);
            }
        }
コード例 #10
0
        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);
                }
            });
        }
コード例 #11
0
ファイル: MainWindow.xaml.cs プロジェクト: dylandee/Wox
 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);
     }
 }