mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-23 06:25:19 -07:00
Add keyboard support
This commit is contained in:
parent
135500d6c7
commit
ca45a2ee61
6 changed files with 115 additions and 7 deletions
|
@ -1,8 +1,9 @@
|
||||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
// Licensed under the MIT License.
|
// Licensed under the MIT License.
|
||||||
|
|
||||||
|
using System;
|
||||||
using Windows.UI.Xaml;
|
using Windows.UI.Xaml;
|
||||||
|
using CalculatorApp.ViewModel;
|
||||||
|
|
||||||
namespace CalculatorApp
|
namespace CalculatorApp
|
||||||
{
|
{
|
||||||
|
@ -91,6 +92,23 @@ namespace CalculatorApp
|
||||||
// Using a DependencyProperty as the backing store for VirtualKeyControlInverseChord. This enables animation, styling, binding, etc...
|
// Using a DependencyProperty as the backing store for VirtualKeyControlInverseChord. This enables animation, styling, binding, etc...
|
||||||
public static readonly DependencyProperty VirtualKeyControlInverseChordProperty =
|
public static readonly DependencyProperty VirtualKeyControlInverseChordProperty =
|
||||||
DependencyProperty.RegisterAttached("VirtualKeyControlInverseChord", typeof(MyVirtualKey), typeof(KeyboardShortcutManager), new PropertyMetadata(MyVirtualKey.None));
|
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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -217,6 +217,22 @@ namespace CalculatorApp.ViewModel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
void SetMenuCategories()
|
||||||
{
|
{
|
||||||
// Use the Categories property instead of the backing variable
|
// Use the Categories property instead of the backing variable
|
||||||
|
|
|
@ -14,6 +14,7 @@ using Windows.Foundation;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using CalculatorApp;
|
using CalculatorApp;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using Windows.System;
|
||||||
|
|
||||||
namespace CalculatorApp.ViewModel
|
namespace CalculatorApp.ViewModel
|
||||||
{
|
{
|
||||||
|
@ -369,6 +370,12 @@ namespace CalculatorApp.ViewModel
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO UNO: KeyboardShortcutManager
|
||||||
|
public void OnKeyPress(VirtualKey key)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
// PRIVATE
|
// PRIVATE
|
||||||
private void OnPropertyChanged(string prop)
|
private void OnPropertyChanged(string prop)
|
||||||
{
|
{
|
||||||
|
|
|
@ -24,6 +24,7 @@ using System.Linq;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Calculator;
|
using Calculator;
|
||||||
|
using Windows.UI.Xaml.Input;
|
||||||
|
|
||||||
namespace CalculatorApp.ViewModel
|
namespace CalculatorApp.ViewModel
|
||||||
{
|
{
|
||||||
|
@ -1174,6 +1175,43 @@ namespace CalculatorApp.ViewModel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static readonly IDictionary<VirtualKey, Command> _keyToCommandMap = new Dictionary<VirtualKey, Command>
|
||||||
|
{
|
||||||
|
{ 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)
|
public void OnCopyCommand(object parameter)
|
||||||
{
|
{
|
||||||
CopyPasteManager.CopyToClipboard(GetRawDisplayValue());
|
CopyPasteManager.CopyToClipboard(GetRawDisplayValue());
|
||||||
|
|
|
@ -31,6 +31,7 @@ using CategorySelectionInitializer = System.Tuple<CalculatorApp.CalculatorList<U
|
||||||
using UnitToUnitToConversionDataMap = System.Collections.Generic.Dictionary<UnitConversionManager.Unit, System.Collections.Generic.Dictionary<UnitConversionManager.Unit, UnitConversionManager.ConversionData>>;
|
using UnitToUnitToConversionDataMap = System.Collections.Generic.Dictionary<UnitConversionManager.Unit, System.Collections.Generic.Dictionary<UnitConversionManager.Unit, UnitConversionManager.ConversionData>>;
|
||||||
using CategoryToUnitVectorMap = System.Collections.Generic.Dictionary<UnitConversionManager.Category, CalculatorApp.CalculatorList<UnitConversionManager.Unit>>;
|
using CategoryToUnitVectorMap = System.Collections.Generic.Dictionary<UnitConversionManager.Category, CalculatorApp.CalculatorList<UnitConversionManager.Unit>>;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using Windows.System;
|
||||||
|
|
||||||
namespace CalculatorApp.ViewModel
|
namespace CalculatorApp.ViewModel
|
||||||
{
|
{
|
||||||
|
@ -1711,6 +1712,34 @@ namespace CalculatorApp.ViewModel
|
||||||
return mappedValue;
|
return mappedValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static readonly IDictionary<VirtualKey, UCM.Command> _keyToCommandMap = new Dictionary<VirtualKey, UCM.Command>
|
||||||
|
{
|
||||||
|
{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)
|
public void OnPaste(String stringToPaste, ViewMode mode)
|
||||||
{
|
{
|
||||||
// If pastedString is invalid("NoOp") then display pasteError else process the string
|
// If pastedString is invalid("NoOp") then display pasteError else process the string
|
||||||
|
|
|
@ -13,6 +13,7 @@ using Windows.Foundation;
|
||||||
using Windows.Foundation.Collections;
|
using Windows.Foundation.Collections;
|
||||||
using Windows.Graphics.Display;
|
using Windows.Graphics.Display;
|
||||||
using Windows.Storage;
|
using Windows.Storage;
|
||||||
|
using Windows.System;
|
||||||
using Windows.UI.Core;
|
using Windows.UI.Core;
|
||||||
using Windows.UI.Xaml;
|
using Windows.UI.Xaml;
|
||||||
using Windows.UI.Xaml.Automation;
|
using Windows.UI.Xaml.Automation;
|
||||||
|
@ -55,8 +56,7 @@ namespace CalculatorApp
|
||||||
this.InitializeComponent();
|
this.InitializeComponent();
|
||||||
m_model = new ApplicationViewModel();
|
m_model = new ApplicationViewModel();
|
||||||
|
|
||||||
// UNO TODO
|
KeyboardShortcutManager.Initialize(m_model);
|
||||||
// KeyboardShortcutManager.Initialize();
|
|
||||||
|
|
||||||
m_model.PropertyChanged += OnAppPropertyChanged;
|
m_model.PropertyChanged += OnAppPropertyChanged;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue