2014-02-20 23:15:03 +08:00
|
|
|
|
using System;
|
2016-01-06 14:31:17 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2014-02-20 23:15:03 +08:00
|
|
|
|
using System.Windows;
|
2015-06-07 11:45:06 +08:00
|
|
|
|
using System.Windows.Controls;
|
2014-02-20 23:15:03 +08:00
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using System.Windows.Media;
|
2014-02-22 11:55:48 +08:00
|
|
|
|
using NHotkey.Wpf;
|
2016-01-08 04:04:37 +08:00
|
|
|
|
using Wox.Core.Resource;
|
2014-12-21 22:03:03 +08:00
|
|
|
|
using Wox.Infrastructure.Hotkey;
|
2014-02-20 23:15:03 +08:00
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
|
2015-06-07 11:45:06 +08:00
|
|
|
|
public event EventHandler HotkeyChanged;
|
2014-02-22 11:55:48 +08:00
|
|
|
|
|
2015-06-07 11:45:06 +08:00
|
|
|
|
protected virtual void OnHotkeyChanged()
|
2014-02-22 11:55:48 +08:00
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
EventHandler handler = HotkeyChanged;
|
2014-02-22 11:55:48 +08:00
|
|
|
|
if (handler != null) handler(this, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 23:15:03 +08:00
|
|
|
|
public HotkeyControl()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-07 11:45:06 +08:00
|
|
|
|
void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e)
|
2014-02-20 23:15:03 +08:00
|
|
|
|
{
|
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);
|
|
|
|
|
|
2015-01-08 22:49:42 +08:00
|
|
|
|
SpecialKeyState specialKeyState = GlobalHotkey.Instance.CheckModifiers();
|
2014-02-22 11:55:48 +08:00
|
|
|
|
|
2015-06-07 11:45:06 +08:00
|
|
|
|
var hotkeyModel = new HotkeyModel(
|
2016-01-06 14:31:17 +08:00
|
|
|
|
specialKeyState.AltPressed,
|
|
|
|
|
specialKeyState.ShiftPressed,
|
|
|
|
|
specialKeyState.WinPressed,
|
|
|
|
|
specialKeyState.CtrlPressed,
|
2015-06-07 11:45:06 +08:00
|
|
|
|
key);
|
|
|
|
|
|
|
|
|
|
var hotkeyString = hotkeyModel.ToString();
|
2014-02-22 15:52:20 +08:00
|
|
|
|
|
2015-06-07 11:45:06 +08:00
|
|
|
|
if (hotkeyString == tbHotkey.Text)
|
2014-02-22 15:52:20 +08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-02-22 11:55:48 +08:00
|
|
|
|
|
2016-01-06 14:31:17 +08:00
|
|
|
|
Dispatcher.InvokeAsync(async () =>
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(500);
|
|
|
|
|
SetHotkey(hotkeyModel);
|
|
|
|
|
});
|
2014-02-22 11:55:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-07 11:45:06 +08:00
|
|
|
|
public void SetHotkey(HotkeyModel keyModel, bool triggerValidate = true)
|
2014-02-22 11:55:48 +08:00
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
CurrentHotkey = keyModel;
|
|
|
|
|
|
|
|
|
|
tbHotkey.Text = CurrentHotkey.ToString();
|
2014-02-22 11:55:48 +08:00
|
|
|
|
tbHotkey.Select(tbHotkey.Text.Length, 0);
|
2014-02-22 15:52:20 +08:00
|
|
|
|
|
|
|
|
|
if (triggerValidate)
|
2014-02-22 11:55:48 +08:00
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
CurrentHotkeyAvailable = CheckHotkeyAvailability();
|
2014-02-22 15:52:20 +08:00
|
|
|
|
if (!CurrentHotkeyAvailable)
|
|
|
|
|
{
|
|
|
|
|
tbMsg.Foreground = new SolidColorBrush(Colors.Red);
|
2015-01-21 23:00:56 +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-21 23:00:56 +08:00
|
|
|
|
tbMsg.Text = InternationalizationManager.Instance.GetTranslation("succeed");
|
2014-02-22 15:52:20 +08:00
|
|
|
|
}
|
2015-06-07 11:45:06 +08:00
|
|
|
|
tbMsg.Visibility = Visibility.Visible;
|
|
|
|
|
OnHotkeyChanged();
|
2014-02-22 11:55:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-07 11:45:06 +08:00
|
|
|
|
public void SetHotkey(string keyStr, bool triggerValidate = true)
|
|
|
|
|
{
|
|
|
|
|
SetHotkey(new HotkeyModel(keyStr), triggerValidate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CheckHotkeyAvailability()
|
2014-02-22 11:55:48 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-01-06 14:31:17 +08:00
|
|
|
|
HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", CurrentHotkey.CharKey, CurrentHotkey.ModifierKeys, (sender, e) => { });
|
2014-02-22 11:55:48 +08:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2015-06-07 11:45:06 +08:00
|
|
|
|
HotkeyManager.Current.Remove("HotkeyAvailabilityTest");
|
2014-02-22 11:55:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-09-26 05:50:22 +08:00
|
|
|
|
|
|
|
|
|
public new bool IsFocused
|
|
|
|
|
{
|
|
|
|
|
get { return tbHotkey.IsFocused; }
|
|
|
|
|
}
|
2014-02-20 23:15:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|