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);
                }
            });
        }
Esempio n. 2
0
        private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            //when alt is pressed, the real key should be e.SystemKey
            Key key = (e.Key == Key.System ? e.SystemKey : e.Key);

            switch (key)
            {
            case Key.Escape:
                HideWox();
                e.Handled = true;
                break;

            case Key.Tab:
                if (globalHotkey.CheckModifiers().ShiftPressed)
                {
                    SelectPrevItem();
                }
                else
                {
                    SelectNextItem();
                }
                e.Handled = true;
                break;

            case Key.Down:
                SelectNextItem();
                e.Handled = true;
                break;

            case Key.Up:
                SelectPrevItem();
                e.Handled = true;
                break;

            case Key.PageDown:
                resultCtrl.SelectNextPage();
                if (IsCMDMode)
                {
                    updateCmdMode();
                }
                toolTip.IsOpen = false;
                e.Handled      = true;
                break;

            case Key.PageUp:
                resultCtrl.SelectPrevPage();
                if (IsCMDMode)
                {
                    updateCmdMode();
                }
                toolTip.IsOpen = false;
                e.Handled      = true;
                break;

            case Key.Back:
                if (BackKeyDownEvent != null)
                {
                    BackKeyDownEvent(tbQuery, new WoxKeyDownEventArgs()
                    {
                        Query        = tbQuery.Text,
                        keyEventArgs = e
                    });
                }
                break;

            case Key.F1:
                Process.Start("https://github.com/qianlifeng/Wox/wiki/Wox-Function-Guide");
                break;

            case Key.Enter:
                AcceptSelect(resultCtrl.GetActiveResult());
                e.Handled = true;
                break;
            }
        }
        private void InitializeKeyCommands()
        {
            EscCommand = new RelayCommand(_ =>
            {
                if (!SelectedIsFromQueryResults())
                {
                    SelectedResults = Results;
                }
                else
                {
                    Hide();
                }
            });

            ClearQueryCommand = new RelayCommand(_ =>
            {
                if (!string.IsNullOrEmpty(QueryText))
                {
                    ChangeQueryText(string.Empty);

                    // Push Event to UI SystemQuery has changed
                    //OnPropertyChanged(nameof(SystemQueryText));
                }
            });

            SelectNextItemCommand = new RelayCommand(_ => { SelectedResults.SelectNextResult(); });

            SelectPrevItemCommand = new RelayCommand(_ => { SelectedResults.SelectPrevResult(); });

            SelectNextPageCommand = new RelayCommand(_ => { SelectedResults.SelectNextPage(); });

            SelectPrevPageCommand = new RelayCommand(_ => { SelectedResults.SelectPrevPage(); });

            SelectFirstResultCommand = new RelayCommand(_ => SelectedResults.SelectFirstResult());

            StartHelpCommand = new RelayCommand(_ =>
            {
                PluginManager.API.OpenUrl("https://github.com/Flow-Launcher/Flow.Launcher/wiki/Flow-Launcher/");
            });
            OpenSettingCommand = new RelayCommand(_ => { App.API.OpenSettingDialog(); });
            OpenResultCommand  = new RelayCommand(index =>
            {
                var results = SelectedResults;

                if (index != null)
                {
                    results.SelectedIndex = int.Parse(index.ToString());
                }

                var result = results.SelectedItem?.Result;
                if (result != null) // SelectedItem returns null if selection is empty.
                {
                    bool hideWindow = result.Action != null && result.Action(new ActionContext
                    {
                        SpecialKeyState = GlobalHotkey.CheckModifiers()
                    });

                    if (hideWindow)
                    {
                        Hide();
                    }

                    if (SelectedIsFromQueryResults())
                    {
                        _userSelectedRecord.Add(result);
                        _history.Add(result.OriginQuery.RawQuery);
                    }
                    else
                    {
                        SelectedResults = Results;
                    }
                }
            });

            AutocompleteQueryCommand = new RelayCommand(_ =>
            {
                var result = SelectedResults.SelectedItem?.Result;
                if (result != null) // SelectedItem returns null if selection is empty.
                {
                    var autoCompleteText = result.Title;

                    if (!string.IsNullOrEmpty(result.AutoCompleteText))
                    {
                        autoCompleteText = result.AutoCompleteText;
                    }
                    else if (!string.IsNullOrEmpty(SelectedResults.SelectedItem?.QuerySuggestionText))
                    {
                        autoCompleteText = SelectedResults.SelectedItem.QuerySuggestionText;
                    }

                    var specialKeyState = GlobalHotkey.CheckModifiers();
                    if (specialKeyState.ShiftPressed)
                    {
                        autoCompleteText = result.SubTitle;
                    }

                    ChangeQueryText(autoCompleteText);
                }
            });

            LoadContextMenuCommand = new RelayCommand(_ =>
            {
                if (SelectedIsFromQueryResults())
                {
                    // When switch to ContextMenu from QueryResults, but no item being chosen, should do nothing
                    // i.e. Shift+Enter/Ctrl+O right after Alt + Space should do nothing
                    if (SelectedResults.SelectedItem != null)
                    {
                        SelectedResults = ContextMenu;
                    }
                }
                else
                {
                    SelectedResults = Results;
                }
            });

            LoadHistoryCommand = new RelayCommand(_ =>
            {
                if (SelectedIsFromQueryResults())
                {
                    SelectedResults       = History;
                    History.SelectedIndex = _history.Items.Count - 1;
                }
                else
                {
                    SelectedResults = Results;
                }
            });

            ReloadPluginDataCommand = new RelayCommand(_ =>
            {
                Hide();

                PluginManager
                .ReloadData()
                .ContinueWith(_ =>
                              Application.Current.Dispatcher.Invoke(() =>
                {
                    Notification.Show(
                        InternationalizationManager.Instance.GetTranslation("success"),
                        InternationalizationManager.Instance.GetTranslation("completedSuccessfully"),
                        "");
                }))
                .ConfigureAwait(false);
            });
        }