Skip to content

Commit 376f810

Browse files
authored
Use KeyDown instead of KeyboardAccelerator for command's hotkeys. (#11653)
1 parent 1a17fc5 commit 376f810

File tree

2 files changed

+61
-52
lines changed

2 files changed

+61
-52
lines changed

src/Files.App/Commands/HotKey.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace Files.App.Commands
3232
[VirtualKey.NumberPad8] = "Pad8",
3333
[VirtualKey.NumberPad9] = "Pad9",
3434
[VirtualKey.Delete] = "Del",
35+
[(VirtualKey)192] = "`",
3536
}.ToImmutableDictionary();
3637

3738
public static HotKey None { get; } = new(VirtualKey.None, VirtualKeyModifiers.None);

src/Files.App/Views/MainPage.xaml.cs

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using CommunityToolkit.Mvvm.DependencyInjection;
22
using CommunityToolkit.Mvvm.Input;
33
using CommunityToolkit.WinUI.Helpers;
4+
using CommunityToolkit.WinUI.UI;
45
using CommunityToolkit.WinUI.UI.Controls;
56
using Files.App.Commands;
67
using Files.App.DataModels;
@@ -22,7 +23,6 @@
2223
using Microsoft.UI.Xaml.Navigation;
2324
using System;
2425
using System.ComponentModel;
25-
using System.Linq;
2626
using System.Runtime.CompilerServices;
2727
using System.Threading.Tasks;
2828
using System.Windows.Input;
@@ -31,6 +31,7 @@
3131
using Windows.Graphics;
3232
using Windows.Services.Store;
3333
using Windows.Storage;
34+
using Windows.System;
3435

3536
namespace Files.App.Views
3637
{
@@ -39,6 +40,8 @@ namespace Files.App.Views
3940
/// </summary>
4041
public sealed partial class MainPage : Page, INotifyPropertyChanged
4142
{
43+
private VirtualKeyModifiers currentModifiers = VirtualKeyModifiers.None;
44+
4245
public IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
4346
public ICommandManager Commands { get; } = Ioc.Default.GetRequiredService<ICommandManager>();
4447

@@ -218,6 +221,62 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
218221
SidebarControl.SidebarItemNewPaneInvoked += SidebarControl_SidebarItemNewPaneInvoked;
219222
}
220223

224+
protected override async void OnPreviewKeyDown(KeyRoutedEventArgs e)
225+
{
226+
base.OnPreviewKeyDown(e);
227+
228+
switch (e.Key)
229+
{
230+
case VirtualKey.Menu:
231+
currentModifiers |= VirtualKeyModifiers.Menu;
232+
break;
233+
case VirtualKey.Control:
234+
currentModifiers |= VirtualKeyModifiers.Control;
235+
break;
236+
case VirtualKey.Shift:
237+
currentModifiers |= VirtualKeyModifiers.Shift;
238+
break;
239+
default:
240+
// break for natives hotkeys in textbox (cut/copy/paste/selectAll/cancel)
241+
bool isTextBox = e.OriginalSource is DependencyObject source && source.FindAscendantOrSelf<TextBox>() is not null;
242+
if (isTextBox)
243+
{
244+
if (currentModifiers is VirtualKeyModifiers.Control &&
245+
e.Key is VirtualKey.X or VirtualKey.C or VirtualKey.V or VirtualKey.A or VirtualKey.Z)
246+
{
247+
break;
248+
}
249+
}
250+
251+
// execute command for hotkey
252+
var hotKey = new HotKey(e.Key, currentModifiers);
253+
var command = Commands[hotKey];
254+
if (command.Code is not CommandCodes.None)
255+
{
256+
e.Handled = true;
257+
await command.ExecuteAsync();
258+
}
259+
break;
260+
}
261+
}
262+
protected override void OnPreviewKeyUp(KeyRoutedEventArgs e)
263+
{
264+
base.OnPreviewKeyDown(e);
265+
266+
switch (e.Key)
267+
{
268+
case VirtualKey.Menu:
269+
currentModifiers &= ~VirtualKeyModifiers.Menu;
270+
break;
271+
case VirtualKey.Control:
272+
currentModifiers &= ~VirtualKeyModifiers.Control;
273+
break;
274+
case VirtualKey.Shift:
275+
currentModifiers &= ~VirtualKeyModifiers.Shift;
276+
break;
277+
}
278+
}
279+
221280
private async void SidebarControl_SidebarItemDropped(object sender, SidebarItemDroppedEventArgs e)
222281
{
223282
await SidebarAdaptiveViewModel.FilesystemHelpers.PerformOperationTypeAsync(e.AcceptedOperation, e.Package, e.ItemPath, false, true);
@@ -320,11 +379,6 @@ private void Page_Loaded(object sender, RoutedEventArgs e)
320379
FindName(nameof(TabControl));
321380
FindName(nameof(NavToolbar));
322381

323-
var commands = Commands.Where(command => !command.CustomHotKey.IsNone);
324-
foreach (var command in commands)
325-
KeyboardAccelerators.Add(new CommandAccelerator(command));
326-
Commands.HotKeyChanged += Commands_HotKeyChanged;
327-
328382
if (Package.Current.Id.Name != "49306atecsolution.FilesUWP" || UserSettingsService.ApplicationSettingsService.ClickedToReviewApp)
329383
return;
330384

@@ -513,51 +567,5 @@ private void RootGrid_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
513567
}
514568

515569
private void NavToolbar_Loaded(object sender, RoutedEventArgs e) => UpdateNavToolbarProperties();
516-
517-
private void Commands_HotKeyChanged(object? sender, HotKeyChangedEventArgs e)
518-
{
519-
if (!e.OldHotKey.IsNone)
520-
{
521-
var oldAccelerator = KeyboardAccelerators.FirstOrDefault(IsOldHotKey);
522-
if (oldAccelerator is CommandAccelerator commandAccelerator)
523-
{
524-
commandAccelerator.Dispose();
525-
KeyboardAccelerators.Remove(commandAccelerator);
526-
}
527-
}
528-
529-
if (!e.NewHotKey.IsNone)
530-
{
531-
var newAccelerator = new CommandAccelerator(e.Command);
532-
KeyboardAccelerators.Add(newAccelerator);
533-
}
534-
535-
bool IsOldHotKey(KeyboardAccelerator accelerator)
536-
=> accelerator is CommandAccelerator commandAccelerator
537-
&& accelerator.Key == e.OldHotKey.Key
538-
&& accelerator.Modifiers == e.OldHotKey.Modifiers;
539-
}
540-
541-
private class CommandAccelerator : KeyboardAccelerator, IDisposable
542-
{
543-
public IRichCommand Command { get; }
544-
545-
public CommandAccelerator(IRichCommand command)
546-
{
547-
Command = command;
548-
549-
Key = Command.CustomHotKey.Key;
550-
Modifiers = Command.CustomHotKey.Modifiers;
551-
Invoked += CommandAccelerator_Invoked;
552-
}
553-
554-
public void Dispose() => Invoked -= CommandAccelerator_Invoked;
555-
556-
private async void CommandAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs e)
557-
{
558-
e.Handled = true;
559-
await Command.ExecuteAsync();
560-
}
561-
}
562570
}
563571
}

0 commit comments

Comments
 (0)