-
-
Notifications
You must be signed in to change notification settings - Fork 464
/
KeyboardCommandHandler.cs
46 lines (39 loc) · 1.54 KB
/
KeyboardCommandHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace Notepads.Commands
{
using System.Collections.Generic;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
public class KeyboardCommandHandler : IKeyboardCommandHandler<KeyRoutedEventArgs>
{
public readonly ICollection<IKeyboardCommand<KeyRoutedEventArgs>> Commands;
private IKeyboardCommand<KeyRoutedEventArgs> _lastCommand;
public KeyboardCommandHandler(ICollection<IKeyboardCommand<KeyRoutedEventArgs>> commands)
{
Commands = commands;
}
public void Handle(KeyRoutedEventArgs args)
{
var ctrlDown = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
var altDown = Window.Current.CoreWindow.GetKeyState(VirtualKey.Menu).HasFlag(CoreVirtualKeyStates.Down);
var shiftDown = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
foreach (var keyboardCommand in Commands)
{
if (keyboardCommand.Hit(ctrlDown, altDown, shiftDown, args.Key))
{
if (keyboardCommand.ShouldExecute(_lastCommand))
{
keyboardCommand.Execute(args);
}
args.Handled = true;
_lastCommand = keyboardCommand;
}
}
if (!args.Handled)
{
_lastCommand = null;
}
}
}
}