From ca45a2ee6112984568dc2e12bfd8295f142f6cec Mon Sep 17 00:00:00 2001 From: "Dr.Rx" Date: Mon, 27 May 2019 16:00:13 -0400 Subject: [PATCH] Add keyboard support --- .../Controls/KeyboardShortcutManager.cs | 22 +++++++++- .../ViewModels/ApplicationViewModel.cs | 20 +++++++++- .../ViewModels/DateCalculatorViewModel.cs | 7 ++++ .../ViewModels/StandardCalculatorViewModel.cs | 40 ++++++++++++++++++- .../ViewModels/UnitConverterViewModel.cs | 29 ++++++++++++++ src/Calculator.Shared/Views/MainPage.xaml.cs | 4 +- 6 files changed, 115 insertions(+), 7 deletions(-) diff --git a/src/Calculator.Shared/Controls/KeyboardShortcutManager.cs b/src/Calculator.Shared/Controls/KeyboardShortcutManager.cs index d16a97c8..b3febfd1 100644 --- a/src/Calculator.Shared/Controls/KeyboardShortcutManager.cs +++ b/src/Calculator.Shared/Controls/KeyboardShortcutManager.cs @@ -1,8 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. - +using System; using Windows.UI.Xaml; +using CalculatorApp.ViewModel; namespace CalculatorApp { @@ -91,6 +92,23 @@ namespace CalculatorApp // Using a DependencyProperty as the backing store for VirtualKeyControlInverseChord. This enables animation, styling, binding, etc... public static readonly DependencyProperty VirtualKeyControlInverseChordProperty = DependencyProperty.RegisterAttached("VirtualKeyControlInverseChord", typeof(MyVirtualKey), typeof(KeyboardShortcutManager), new PropertyMetadata(MyVirtualKey.None)); - } + + // TODO UNO + public static void Initialize(ApplicationViewModel target) + { + var coreWindow = Window.Current.Content; + try + { + coreWindow.KeyDown += (snd, e) => + { + target.OnKeyPress(e.Key); + e.Handled = true; + }; + } + catch (Exception) + { + } + } + } } } diff --git a/src/Calculator.Shared/ViewModels/ApplicationViewModel.cs b/src/Calculator.Shared/ViewModels/ApplicationViewModel.cs index a859574a..0b7f50dd 100644 --- a/src/Calculator.Shared/ViewModels/ApplicationViewModel.cs +++ b/src/Calculator.Shared/ViewModels/ApplicationViewModel.cs @@ -217,12 +217,28 @@ namespace CalculatorApp.ViewModel } } - void SetMenuCategories() + public void OnKeyPress(VirtualKey key) + { + if (NavCategory.IsConverterViewMode(m_mode)) + { + ConverterViewModel.OnKeyPress(key); + } + else if (NavCategory.IsDateCalculatorViewMode(m_mode)) + { + DateCalcViewModel.OnKeyPress(key); + } + else + { + CalculatorViewModel.OnKeyPress(key); + } + } + + void SetMenuCategories() { // Use the Categories property instead of the backing variable // because we want to take advantage of binding updates and // property setter logic. Categories = NavCategoryGroup.CreateMenuOptions(); } - } + } } diff --git a/src/Calculator.Shared/ViewModels/DateCalculatorViewModel.cs b/src/Calculator.Shared/ViewModels/DateCalculatorViewModel.cs index 73eb293d..1b7c3e12 100644 --- a/src/Calculator.Shared/ViewModels/DateCalculatorViewModel.cs +++ b/src/Calculator.Shared/ViewModels/DateCalculatorViewModel.cs @@ -14,6 +14,7 @@ using Windows.Foundation; using System.Numerics; using CalculatorApp; using System.Globalization; +using Windows.System; namespace CalculatorApp.ViewModel { @@ -369,6 +370,12 @@ namespace CalculatorApp.ViewModel } + + // TODO UNO: KeyboardShortcutManager + public void OnKeyPress(VirtualKey key) + { + } + // PRIVATE private void OnPropertyChanged(string prop) { diff --git a/src/Calculator.Shared/ViewModels/StandardCalculatorViewModel.cs b/src/Calculator.Shared/ViewModels/StandardCalculatorViewModel.cs index c5e5d501..94fb827a 100644 --- a/src/Calculator.Shared/ViewModels/StandardCalculatorViewModel.cs +++ b/src/Calculator.Shared/ViewModels/StandardCalculatorViewModel.cs @@ -24,6 +24,7 @@ using System.Linq; using System.Collections.ObjectModel; using System.Threading.Tasks; using Calculator; +using Windows.UI.Xaml.Input; namespace CalculatorApp.ViewModel { @@ -1174,7 +1175,44 @@ namespace CalculatorApp.ViewModel } } - public void OnCopyCommand(object parameter) + private static readonly IDictionary _keyToCommandMap = new Dictionary + { + { VirtualKey.Number0, Command.Command0 }, + { VirtualKey.Number1, Command.Command1 }, + { VirtualKey.Number2, Command.Command2 }, + { VirtualKey.Number3, Command.Command3 }, + { VirtualKey.Number4, Command.Command4 }, + { VirtualKey.Number5, Command.Command5 }, + { VirtualKey.Number6, Command.Command6 }, + { VirtualKey.Number7, Command.Command7 }, + { VirtualKey.Number8, Command.Command8 }, + { VirtualKey.Number9, Command.Command9 }, + { VirtualKey.A, Command.CommandA }, + { VirtualKey.B, Command.CommandB }, + { VirtualKey.C, Command.CommandC }, + { VirtualKey.D, Command.CommandD }, + { VirtualKey.E, Command.CommandE }, + { VirtualKey.F, Command.CommandF }, + { VirtualKey.Decimal, Command.CommandPNT }, + { VirtualKey.Add, Command.CommandADD }, + { VirtualKey.Subtract, Command.CommandSUB }, + { VirtualKey.Multiply, Command.CommandMUL }, + { VirtualKey.Divide, Command.CommandDIV }, + { VirtualKey.Enter, Command.CommandEQU }, + { VirtualKey.Back, Command.CommandBACK }, + { VirtualKey.Escape, Command.CommandCLEAR }, + }; + + // TODO UNO: KeyboardShortcutManager + public void OnKeyPress(VirtualKey key) + { + if (_keyToCommandMap.TryGetValue(key, out var cmd)) + { + m_standardCalculatorManager.SendCommand(cmd); + } + } + + public void OnCopyCommand(object parameter) { CopyPasteManager.CopyToClipboard(GetRawDisplayValue()); diff --git a/src/Calculator.Shared/ViewModels/UnitConverterViewModel.cs b/src/Calculator.Shared/ViewModels/UnitConverterViewModel.cs index e0dcc683..8b668311 100644 --- a/src/Calculator.Shared/ViewModels/UnitConverterViewModel.cs +++ b/src/Calculator.Shared/ViewModels/UnitConverterViewModel.cs @@ -31,6 +31,7 @@ using CategorySelectionInitializer = System.Tuple>; using CategoryToUnitVectorMap = System.Collections.Generic.Dictionary>; using System.Globalization; +using Windows.System; namespace CalculatorApp.ViewModel { @@ -1711,6 +1712,34 @@ namespace CalculatorApp.ViewModel return mappedValue; } + private static readonly IDictionary _keyToCommandMap = new Dictionary + { + {VirtualKey.Number0, UCM.Command.Zero}, + {VirtualKey.Number1, UCM.Command.One}, + {VirtualKey.Number2, UCM.Command.Two}, + {VirtualKey.Number3, UCM.Command.Three}, + {VirtualKey.Number4, UCM.Command.Four}, + {VirtualKey.Number5, UCM.Command.Five}, + {VirtualKey.Number6, UCM.Command.Six}, + {VirtualKey.Number7, UCM.Command.Seven}, + {VirtualKey.Number8, UCM.Command.Eight}, + {VirtualKey.Number9, UCM.Command.Nine}, + {VirtualKey.Decimal, UCM.Command.Decimal}, + {VirtualKey.Subtract, UCM.Command.Negate}, + {VirtualKey.Back, UCM.Command.Backspace}, + {VirtualKey.Escape, UCM.Command.Clear}, + {VirtualKey.Delete, UCM.Command.Reset}, + }; + + // TODO UNO: KeyboardShortcutManager + public void OnKeyPress(VirtualKey key) + { + if (_keyToCommandMap.TryGetValue(key, out var cmd)) + { + m_model.SendCommand(cmd); + } + } + public void OnPaste(String stringToPaste, ViewMode mode) { // If pastedString is invalid("NoOp") then display pasteError else process the string diff --git a/src/Calculator.Shared/Views/MainPage.xaml.cs b/src/Calculator.Shared/Views/MainPage.xaml.cs index 9977fbc4..16ac1a3b 100644 --- a/src/Calculator.Shared/Views/MainPage.xaml.cs +++ b/src/Calculator.Shared/Views/MainPage.xaml.cs @@ -13,6 +13,7 @@ using Windows.Foundation; using Windows.Foundation.Collections; using Windows.Graphics.Display; using Windows.Storage; +using Windows.System; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Automation; @@ -55,8 +56,7 @@ namespace CalculatorApp this.InitializeComponent(); m_model = new ApplicationViewModel(); - // UNO TODO - // KeyboardShortcutManager.Initialize(); + KeyboardShortcutManager.Initialize(m_model); m_model.PropertyChanged += OnAppPropertyChanged;