PowerToys/Wox/HotkeyControl.xaml.cs

143 lines
4.1 KiB
C#
Raw Normal View History

2014-02-20 23:15:03 +08:00
using System;
2014-02-22 11:55:48 +08:00
using System.Drawing;
2014-02-22 15:52:20 +08:00
using System.Runtime.InteropServices;
2014-02-20 23:15:03 +08:00
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
2014-02-22 11:55:48 +08:00
using NHotkey;
using NHotkey.Wpf;
2015-01-02 23:07:49 +08:00
using Wox.Core.i18n;
2014-02-20 23:15:03 +08:00
using Wox.Helper;
2014-02-22 11:55:48 +08:00
using Wox.Infrastructure;
2014-12-21 22:03:03 +08:00
using Wox.Infrastructure.Hotkey;
2014-02-20 23:15:03 +08:00
using Wox.Plugin;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using UserControl = System.Windows.Controls.UserControl;
namespace Wox
{
public partial class HotkeyControl : UserControl
{
2014-02-22 11:55:48 +08:00
public HotkeyModel CurrentHotkey { get; private set; }
public bool CurrentHotkeyAvailable { get; private set; }
public event EventHandler OnHotkeyChanged;
protected virtual void OnOnHotkeyChanged()
{
EventHandler handler = OnHotkeyChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
2014-02-20 23:15:03 +08:00
public HotkeyControl()
{
InitializeComponent();
}
private void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
2014-02-22 15:52:20 +08:00
e.Handled = true;
2014-02-22 11:55:48 +08:00
tbMsg.Visibility = Visibility.Hidden;
2014-02-20 23:15:03 +08:00
//when alt is pressed, the real key should be e.SystemKey
Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
string text = string.Empty;
2014-03-19 00:26:09 +08:00
SpecialKeyState specialKeyState = new GlobalHotkey().CheckModifiers();
2014-02-20 23:15:03 +08:00
if (specialKeyState.AltPressed)
{
2014-02-22 11:55:48 +08:00
text += "Alt";
2014-02-20 23:15:03 +08:00
}
if (specialKeyState.CtrlPressed)
{
2014-02-22 11:55:48 +08:00
text += string.IsNullOrEmpty(text) ? "Ctrl" : " + Ctrl";
2014-02-20 23:15:03 +08:00
}
if (specialKeyState.ShiftPressed)
{
2014-02-22 11:55:48 +08:00
text += string.IsNullOrEmpty(text) ? "Shift" : " + Shift";
2014-02-20 23:15:03 +08:00
}
if (specialKeyState.WinPressed)
{
2014-02-22 11:55:48 +08:00
text += string.IsNullOrEmpty(text) ? "Win" : " + Win";
2014-02-20 23:15:03 +08:00
}
2014-02-22 11:55:48 +08:00
if (string.IsNullOrEmpty(text))
2014-02-20 23:15:03 +08:00
{
2014-02-22 11:55:48 +08:00
text += "Ctrl + Alt";
2014-02-20 23:15:03 +08:00
}
2014-02-22 11:55:48 +08:00
if (IsKeyACharOrNumber(key))
2014-02-20 23:15:03 +08:00
{
2014-02-22 11:55:48 +08:00
text += " + " + key;
2014-02-20 23:15:03 +08:00
}
2014-02-22 11:55:48 +08:00
else if (key == Key.Space)
{
text += " + Space";
}
2014-02-22 15:52:20 +08:00
else
{
return;
}
if (text == tbHotkey.Text)
{
return;
}
2014-02-22 11:55:48 +08:00
Dispatcher.DelayInvoke("HotkeyAvailableTest", o => SetHotkey(text), TimeSpan.FromMilliseconds(300));
}
2014-02-22 15:52:20 +08:00
public void SetHotkey(string keyStr, bool triggerValidate = true)
2014-02-22 11:55:48 +08:00
{
2014-02-22 15:52:20 +08:00
tbMsg.Visibility = Visibility.Visible;
2014-02-22 11:55:48 +08:00
tbHotkey.Text = keyStr;
tbHotkey.Select(tbHotkey.Text.Length, 0);
CurrentHotkey = new HotkeyModel(keyStr);
2014-02-22 15:52:20 +08:00
if (triggerValidate)
2014-02-22 11:55:48 +08:00
{
2014-02-22 15:52:20 +08:00
CurrentHotkeyAvailable = CheckHotAvailabel(CurrentHotkey);
if (!CurrentHotkeyAvailable)
{
tbMsg.Foreground = new SolidColorBrush(Colors.Red);
2015-01-02 23:07:49 +08:00
tbMsg.Text = InternationalizationManager.Instance.GetTranslation("hotkeyUnavailable");
2014-02-22 15:52:20 +08:00
}
else
{
tbMsg.Foreground = new SolidColorBrush(Colors.Green);
2015-01-02 23:07:49 +08:00
tbMsg.Text = InternationalizationManager.Instance.GetTranslation("succeed");
2014-02-22 15:52:20 +08:00
}
OnOnHotkeyChanged();
2014-02-22 11:55:48 +08:00
}
}
private bool CheckHotAvailabel(HotkeyModel hotkey)
{
try
{
HotkeyManager.Current.AddOrReplace("HotkeyAvailableTest", hotkey.CharKey, hotkey.ModifierKeys, OnHotkey);
return true;
}
catch
{
}
finally
{
HotkeyManager.Current.Remove("HotkeyAvailableTest");
}
return false;
}
private void OnHotkey(object sender, HotkeyEventArgs e)
{
2014-02-20 23:15:03 +08:00
}
2014-02-22 11:55:48 +08:00
private static bool IsKeyACharOrNumber(Key key)
2014-02-20 23:15:03 +08:00
{
2014-02-22 11:55:48 +08:00
return (key >= Key.A && key <= Key.Z) || (key >= Key.D0 && key <= Key.D9);
2014-02-20 23:15:03 +08:00
}
}
}