Skip to content

Commit

Permalink
Fix keybindings
Browse files Browse the repository at this point in the history
  • Loading branch information
srwi committed Nov 5, 2023
1 parent 246b030 commit 66ab20d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 40 deletions.
12 changes: 6 additions & 6 deletions EverythingToolbar/Controls/SearchBox.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ private void OnPreviewKeyDown(object sender, KeyEventArgs e)
e.Handled = true;
return;
}
else if (Keyboard.Modifiers == ModifierKeys.None &&
(e.Key == Key.Down || e.Key == Key.Enter ||
e.Key == Key.Home || e.Key == Key.End ||
e.Key == Key.PageDown || e.Key == Key.PageUp ||
e.Key == Key.Up || e.Key == Key.Down))
else if (e.Key == Key.Home || e.Key == Key.End ||
e.Key == Key.PageDown || e.Key == Key.PageUp ||
e.Key == Key.Up || e.Key == Key.Down ||
e.Key == Key.Escape|| e.Key == Key.Enter ||
(e.Key >= Key.D0 && e.Key <= Key.D9))
{
EventDispatcher.Instance.InvokeSearchResultsListViewKeyEvent(this, e);
EventDispatcher.Instance.InvokeGlobalKeyEvent(this, e);
e.Handled = true;
return;
}
Expand Down
30 changes: 19 additions & 11 deletions EverythingToolbar/Controls/SearchResultsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public SearchResultsView()
SearchResultsListView.ItemsSource = EverythingSearch.Instance.SearchResults;
((INotifyCollectionChanged)SearchResultsListView.Items).CollectionChanged += AutoSelectFirstResult;

EventDispatcher.Instance.SearchResultsListViewKeyEvent += OnKeyPressed;
EventDispatcher.Instance.GlobalKeyEvent += OnKeyPressed;
SearchResultsListView.PreviewKeyDown += OnKeyPressed;

// Mouse events and context menu must be added to the ItemContainerStyle each time it gets updated
Expand Down Expand Up @@ -73,12 +73,9 @@ private void OnKeyPressed(object sender, KeyEventArgs e)
{
PreviewSelectedFile();
}
else if (e.Key == Key.Enter)
else if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift) && e.Key == Key.Enter)
{
if (SearchResultsListView.SelectedIndex >= 0)
OpenSelectedSearchResult();
else
SelectNextSearchResult();
RunAsAdmin(this, null);
}
else if (Keyboard.Modifiers == ModifierKeys.Shift && e.Key == Key.Enter)
{
Expand All @@ -92,9 +89,12 @@ private void OnKeyPressed(object sender, KeyEventArgs e)
{
OpenFilePath(this, null);
}
else if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift) && e.Key == Key.Enter)
else if (e.Key == Key.Enter)
{
RunAsAdmin(this, null);
if (SearchResultsListView.SelectedIndex >= 0)
OpenSelectedSearchResult();
else
SelectNextSearchResult();
}
else if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift) && e.Key == Key.C)
{
Expand Down Expand Up @@ -199,11 +199,9 @@ private void SelectNthSearchResult(int n)
if (n < 0 || n >= SearchResultsListView.Items.Count)
return;

if (!Settings.Default.isAutoSelectFirstResult)
SearchResultsListView.Focus();

SearchResultsListView.SelectedIndex = n;
SearchResultsListView.ScrollIntoView(SelectedItem);
FocusSelectedItem();
}

private int GetPageSize()
Expand Down Expand Up @@ -401,5 +399,15 @@ private void OnContextMenuOpened(object sender, RoutedEventArgs e)

mi.Visibility = isExecutable ? Visibility.Visible : Visibility.Collapsed;
}

private void FocusSelectedItem()
{
if (Settings.Default.isAutoSelectFirstResult)
return;

var selectedItem = (ListViewItem)SearchResultsListView.ItemContainerGenerator.ContainerFromItem(SelectedItem);
if (selectedItem != null)
Keyboard.Focus(selectedItem);
}
}
}
6 changes: 3 additions & 3 deletions EverythingToolbar/Helpers/EventDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public void InvokeSearchBoxFocused(object sender, EventArgs e)
SearchBoxFocusRequested?.Invoke(sender, e);
}

public event EventHandler<KeyEventArgs> SearchResultsListViewKeyEvent;
public void InvokeSearchResultsListViewKeyEvent(object sender, KeyEventArgs e)
public event EventHandler<KeyEventArgs> GlobalKeyEvent;
public void InvokeGlobalKeyEvent(object sender, KeyEventArgs e)
{
SearchResultsListViewKeyEvent?.Invoke(sender, e);
GlobalKeyEvent?.Invoke(sender, e);
}

public event EventHandler<string> SearchTermReplaced;
Expand Down
42 changes: 22 additions & 20 deletions EverythingToolbar/SearchWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media.Animation;
using System.Windows.Shell;
using EverythingToolbar.Helpers;
Expand Down Expand Up @@ -40,25 +39,8 @@ private SearchWindow()
};
}

PreviewKeyDown += (s, e) =>
{
if (e.Key >= Key.D0 && e.Key <= Key.D9 && Keyboard.Modifiers == ModifierKeys.Control)
{
int index = e.Key == Key.D0 ? 9 : e.Key - Key.D1;
EverythingSearch.Instance.SelectFilterFromIndex(index);
}
else if (e.Key == Key.Escape)
{
Instance.Hide();
Keyboard.ClearFocus();
}
else if (e.Key == Key.Tab)
{
var offset = Keyboard.Modifiers.HasFlag(ModifierKeys.Shift) ? -1 : 1;
EverythingSearch.Instance.CycleFilters(offset);
e.Handled = true;
}
};
EventDispatcher.Instance.GlobalKeyEvent += OnPreviewKeyDown;
PreviewKeyDown += OnPreviewKeyDown;
}

private void OnLoaded(object sender, RoutedEventArgs e)
Expand All @@ -77,6 +59,26 @@ private void OnActivated(object sender, EventArgs e)
EventDispatcher.Instance.InvokeFocusRequested(this, EventArgs.Empty);
}

private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key >= Key.D0 && e.Key <= Key.D9 && Keyboard.Modifiers == ModifierKeys.Control)
{
int index = e.Key == Key.D0 ? 9 : e.Key - Key.D1;
EverythingSearch.Instance.SelectFilterFromIndex(index);
}
else if (e.Key == Key.Escape)
{
Instance.Hide();
Keyboard.ClearFocus();
}
else if (e.Key == Key.Tab)
{
var offset = Keyboard.Modifiers.HasFlag(ModifierKeys.Shift) ? -1 : 1;
EverythingSearch.Instance.CycleFilters(offset);
e.Handled = true;
}
}

private void OnLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (e.NewFocus == null) // New focus outside application
Expand Down

0 comments on commit 66ab20d

Please sign in to comment.