2020-04-08 00:19:00 -07:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Lib;
|
2020-04-03 19:02:38 -07:00
|
|
|
|
using Windows.UI.Core;
|
|
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
|
using Windows.UI.Xaml.Input;
|
|
|
|
|
|
|
|
|
|
|
|
// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236
|
|
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.Controls
|
|
|
|
|
|
{
|
|
|
|
|
|
public sealed partial class HotkeySettingsControl : UserControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public string Header { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty HotkeySettingsProperty =
|
|
|
|
|
|
DependencyProperty.Register(
|
2020-04-08 00:19:00 -07:00
|
|
|
|
"HotkeySettings",
|
2020-04-03 19:02:38 -07:00
|
|
|
|
typeof(HotkeySettings),
|
|
|
|
|
|
typeof(HotkeySettingsControl),
|
|
|
|
|
|
null);
|
|
|
|
|
|
|
2020-04-08 00:19:00 -07:00
|
|
|
|
private HotkeySettings hotkeySettings;
|
|
|
|
|
|
|
|
|
|
|
|
public HotkeySettings HotkeySettings
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return this.hotkeySettings;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
set
|
2020-04-03 19:02:38 -07:00
|
|
|
|
{
|
2020-04-08 00:19:00 -07:00
|
|
|
|
if (this.hotkeySettings != value)
|
2020-04-03 19:02:38 -07:00
|
|
|
|
{
|
2020-04-08 00:19:00 -07:00
|
|
|
|
this.hotkeySettings = value;
|
|
|
|
|
|
this.SetValue(HotkeySettingsProperty, value);
|
|
|
|
|
|
this.HotkeyTextBox.Text = this.HotkeySettings.ToString();
|
2020-04-03 19:02:38 -07:00
|
|
|
|
}
|
2020-04-08 00:19:00 -07:00
|
|
|
|
}
|
2020-04-03 19:02:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public HotkeySettingsControl()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.InitializeComponent();
|
2020-04-08 00:19:00 -07:00
|
|
|
|
this.HotkeyTextBox.PreviewKeyDown += this.HotkeyTextBox_KeyDown;
|
2020-04-03 19:02:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static bool IsDown(Windows.System.VirtualKey key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Window.Current.CoreWindow.GetKeyState(key).HasFlag(CoreVirtualKeyStates.Down);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void HotkeyTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
|
if (
|
|
|
|
|
|
e.Key == Windows.System.VirtualKey.LeftWindows ||
|
|
|
|
|
|
e.Key == Windows.System.VirtualKey.RightWindows ||
|
|
|
|
|
|
e.Key == Windows.System.VirtualKey.Control ||
|
|
|
|
|
|
e.Key == Windows.System.VirtualKey.Menu ||
|
2020-04-08 00:19:00 -07:00
|
|
|
|
e.Key == Windows.System.VirtualKey.Shift)
|
2020-04-03 19:02:38 -07:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var settings = new HotkeySettings();
|
2020-04-08 00:19:00 -07:00
|
|
|
|
|
2020-04-03 19:02:38 -07:00
|
|
|
|
// Display HotKey value
|
2020-04-08 00:19:00 -07:00
|
|
|
|
if (IsDown(Windows.System.VirtualKey.LeftWindows) ||
|
2020-04-03 19:02:38 -07:00
|
|
|
|
IsDown(Windows.System.VirtualKey.RightWindows))
|
|
|
|
|
|
{
|
|
|
|
|
|
settings.win = true;
|
|
|
|
|
|
}
|
2020-04-08 00:19:00 -07:00
|
|
|
|
|
2020-04-03 19:02:38 -07:00
|
|
|
|
if (IsDown(Windows.System.VirtualKey.Control))
|
|
|
|
|
|
{
|
|
|
|
|
|
settings.ctrl = true;
|
|
|
|
|
|
}
|
2020-04-08 00:19:00 -07:00
|
|
|
|
|
2020-04-03 19:02:38 -07:00
|
|
|
|
if (IsDown(Windows.System.VirtualKey.Menu))
|
|
|
|
|
|
{
|
|
|
|
|
|
settings.alt = true;
|
|
|
|
|
|
}
|
2020-04-08 00:19:00 -07:00
|
|
|
|
|
2020-04-03 19:02:38 -07:00
|
|
|
|
if (IsDown(Windows.System.VirtualKey.Shift))
|
|
|
|
|
|
{
|
|
|
|
|
|
settings.shift = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
settings.key = e.Key.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Check that e.OriginalKey is the ScanCode. It is not clear from docs.
|
2020-04-08 00:19:00 -07:00
|
|
|
|
settings.code = (int)e.OriginalKey;
|
|
|
|
|
|
this.HotkeySettings = settings;
|
2020-04-03 19:02:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|