mirror of
https://github.com/Hofknecht/SystemTrayMenu.git
synced 2024-09-06 12:42:02 +12:00
Enable hotkey modification via Settings window
This commit is contained in:
parent
215c263052
commit
d162564e82
3 changed files with 73 additions and 43 deletions
|
@ -40,6 +40,8 @@ namespace SystemTrayMenu.Helpers
|
||||||
|
|
||||||
Key GetKey();
|
Key GetKey();
|
||||||
|
|
||||||
|
string GetHotkeyString();
|
||||||
|
|
||||||
void Register(ModifierKeys modifiers, Key key);
|
void Register(ModifierKeys modifiers, Key key);
|
||||||
|
|
||||||
void Register(string hotKeyString);
|
void Register(string hotKeyString);
|
||||||
|
@ -384,6 +386,39 @@ namespace SystemTrayMenu.Helpers
|
||||||
|
|
||||||
public Key GetKey() => Registration?.Key ?? Key.None;
|
public Key GetKey() => Registration?.Key ?? Key.None;
|
||||||
|
|
||||||
|
public string GetHotkeyString()
|
||||||
|
{
|
||||||
|
string? keyString = null;
|
||||||
|
string? modifiersString = null;
|
||||||
|
ModifierKeys modifiers = GetModifierKeys();
|
||||||
|
Key key = GetKey();
|
||||||
|
|
||||||
|
if (modifiers != ModifierKeys.None)
|
||||||
|
{
|
||||||
|
modifiersString = new ModifierKeysConverter().ConvertToInvariantString(GetModifierKeys());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key != Key.None)
|
||||||
|
{
|
||||||
|
keyString = new KeyConverter().ConvertToInvariantString(GetKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(modifiersString) && string.IsNullOrEmpty(keyString))
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
else if (string.IsNullOrEmpty(modifiersString))
|
||||||
|
{
|
||||||
|
return keyString!;
|
||||||
|
}
|
||||||
|
else if (string.IsNullOrEmpty(keyString))
|
||||||
|
{
|
||||||
|
return modifiersString;
|
||||||
|
}
|
||||||
|
|
||||||
|
return modifiersString + "+" + keyString;
|
||||||
|
}
|
||||||
|
|
||||||
public void Register(ModifierKeys modifiers, Key key)
|
public void Register(ModifierKeys modifiers, Key key)
|
||||||
{
|
{
|
||||||
if (Registration == null)
|
if (Registration == null)
|
||||||
|
|
|
@ -25,10 +25,8 @@ namespace SystemTrayMenu.UserInterface
|
||||||
private readonly IList<int> needNonAltGrModifier = new List<int>();
|
private readonly IList<int> needNonAltGrModifier = new List<int>();
|
||||||
|
|
||||||
// These variables store the current hotkey and modifier(s)
|
// These variables store the current hotkey and modifier(s)
|
||||||
private IHotkeyFunction? hotkeyFunction;
|
|
||||||
private Key hotkey = Key.None;
|
private Key hotkey = Key.None;
|
||||||
private ModifierKeys modifiers = ModifierKeys.None;
|
private ModifierKeys modifiers = ModifierKeys.None;
|
||||||
private Action? handler;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="HotkeySelector"/> class.
|
/// Initializes a new instance of the <see cref="HotkeySelector"/> class.
|
||||||
|
@ -70,6 +68,8 @@ namespace SystemTrayMenu.UserInterface
|
||||||
SetHotkeyRegistration((IHotkeyFunction?)null);
|
SetHotkeyRegistration((IHotkeyFunction?)null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal IHotkeyFunction? HotkeyFunction { get; private set; }
|
||||||
|
|
||||||
public static string HotkeyToString(ModifierKeys modifierKeyCode, Key key)
|
public static string HotkeyToString(ModifierKeys modifierKeyCode, Key key)
|
||||||
{
|
{
|
||||||
StringBuilder hotkeyString = new();
|
StringBuilder hotkeyString = new();
|
||||||
|
@ -104,27 +104,30 @@ namespace SystemTrayMenu.UserInterface
|
||||||
/// <param name="hotkeyFunction">Hotkey function interface.</param>
|
/// <param name="hotkeyFunction">Hotkey function interface.</param>
|
||||||
internal void SetHotkeyRegistration(IHotkeyFunction? hotkeyFunction)
|
internal void SetHotkeyRegistration(IHotkeyFunction? hotkeyFunction)
|
||||||
{
|
{
|
||||||
this.hotkeyFunction = hotkeyFunction;
|
HotkeyFunction = hotkeyFunction;
|
||||||
UpdateHotkeyRegistration();
|
UpdateHotkeyRegistration();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set the hotkey function the control is working on.
|
/// Update the UI based on the hotkey registration.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="hotkeyFunction">Hotkey function interface.</param>
|
|
||||||
internal void UpdateHotkeyRegistration()
|
internal void UpdateHotkeyRegistration()
|
||||||
{
|
{
|
||||||
hotkey = hotkeyFunction?.GetKey() ?? Key.None;
|
hotkey = HotkeyFunction?.GetKey() ?? Key.None;
|
||||||
modifiers = hotkeyFunction?.GetModifierKeys() ?? ModifierKeys.None;
|
modifiers = HotkeyFunction?.GetModifierKeys() ?? ModifierKeys.None;
|
||||||
|
|
||||||
if (modifiers == ModifierKeys.None && hotkey == Key.None)
|
if (modifiers == ModifierKeys.None && hotkey == Key.None)
|
||||||
{
|
{
|
||||||
Background = SystemColors.ControlBrush;
|
Background = SystemColors.ControlBrush;
|
||||||
}
|
}
|
||||||
else
|
else if (HotkeyFunction != null)
|
||||||
{
|
{
|
||||||
Background = Brushes.LightGreen;
|
Background = Brushes.LightGreen;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Background = Brushes.IndianRed;
|
||||||
|
}
|
||||||
|
|
||||||
Text = HotkeyToLocalizedString(modifiers, hotkey);
|
Text = HotkeyToLocalizedString(modifiers, hotkey);
|
||||||
}
|
}
|
||||||
|
@ -133,42 +136,38 @@ namespace SystemTrayMenu.UserInterface
|
||||||
/// Change the hotkey to given combination.
|
/// Change the hotkey to given combination.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="hotkeyString">Hotkey combination string.</param>
|
/// <param name="hotkeyString">Hotkey combination string.</param>
|
||||||
internal void ChangeHotkey(string hotkeyString) => hotkeyFunction?.Register(hotkeyString);
|
internal void ChangeHotkey(string hotkeyString)
|
||||||
|
{
|
||||||
|
HotkeyFunction?.Register(hotkeyString);
|
||||||
|
UpdateHotkeyRegistration();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Register a hotkey.
|
/// Change the hotkey to given combination.
|
||||||
|
/// Sets background accordingly.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="modifiers">The key modifiers .</param>
|
/// <param name="modifiers">Hotkey modifiers.</param>
|
||||||
/// <param name="key">The virtual key code.</param>
|
/// <param name="key">Hotkey key.</param>
|
||||||
/// <param name="handler">A HotKeyHandler, this will be called to handle the hotkey press.</param>
|
internal void ChangeHotkey(ModifierKeys modifiers, Key key)
|
||||||
/// <returns>the hotkey number, -1 if failed.</returns>
|
|
||||||
internal int RegisterHotKey(ModifierKeys modifiers, Key key, Action handler)
|
|
||||||
{
|
{
|
||||||
if (key == Key.None)
|
if (key == Key.None)
|
||||||
{
|
{
|
||||||
Background = SystemColors.ControlBrush;
|
Background = SystemColors.ControlBrush;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
try
|
|
||||||
{
|
{
|
||||||
hotkeyFunction?.Register(modifiers, key);
|
try
|
||||||
}
|
{
|
||||||
catch (InvalidOperationException ex)
|
HotkeyFunction?.Register(modifiers, key);
|
||||||
{
|
}
|
||||||
Background = Brushes.IndianRed;
|
catch (InvalidOperationException ex)
|
||||||
Log.Info($"Couldn't register hotkey modifier {modifiers} key {key} ex: " + ex.ToString());
|
{
|
||||||
return -1;
|
Background = Brushes.IndianRed;
|
||||||
}
|
Log.Info($"Couldn't register hotkey modifier {modifiers} key {key} ex: " + ex.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
this.handler = handler;
|
Background = Brushes.LightGreen;
|
||||||
if (hotkeyFunction != null)
|
|
||||||
{
|
|
||||||
hotkeyFunction.KeyPressed += (_) => handler.Invoke();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Background = Brushes.LightGreen;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -176,7 +175,7 @@ namespace SystemTrayMenu.UserInterface
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void ResetHotkey()
|
private void ResetHotkey()
|
||||||
{
|
{
|
||||||
hotkeyFunction?.Unregister();
|
HotkeyFunction?.Unregister();
|
||||||
UpdateHotkeyRegistration();
|
UpdateHotkeyRegistration();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,7 +202,7 @@ namespace SystemTrayMenu.UserInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Instead of Redraw this seem to act more like an input filter for valid combinations?
|
// TODO: Instead of Redraw this seem to act more like an input filter for valid combinations?
|
||||||
// Maybe move to places right before Register() calls of the hotkeyFunction
|
// Maybe move to places right before Register() calls of the HotkeyFunction
|
||||||
private void Redraw()
|
private void Redraw()
|
||||||
{
|
{
|
||||||
// No modifier or shift only, AND a hotkey that needs another modifier
|
// No modifier or shift only, AND a hotkey that needs another modifier
|
||||||
|
@ -317,7 +316,7 @@ namespace SystemTrayMenu.UserInterface
|
||||||
{
|
{
|
||||||
modifiers = Keyboard.Modifiers;
|
modifiers = Keyboard.Modifiers;
|
||||||
hotkey = e.Key;
|
hotkey = e.Key;
|
||||||
hotkeyFunction?.Register(modifiers, hotkey);
|
ChangeHotkey(modifiers, hotkey);
|
||||||
UpdateHotkeyRegistration();
|
UpdateHotkeyRegistration();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -333,6 +332,7 @@ namespace SystemTrayMenu.UserInterface
|
||||||
{
|
{
|
||||||
modifiers = Keyboard.Modifiers;
|
modifiers = Keyboard.Modifiers;
|
||||||
hotkey = e.Key;
|
hotkey = e.Key;
|
||||||
|
ChangeHotkey(modifiers, hotkey);
|
||||||
UpdateHotkeyRegistration();
|
UpdateHotkeyRegistration();
|
||||||
}
|
}
|
||||||
else if (hotkey == Key.None && modifiers == ModifierKeys.None)
|
else if (hotkey == Key.None && modifiers == ModifierKeys.None)
|
||||||
|
@ -345,10 +345,7 @@ namespace SystemTrayMenu.UserInterface
|
||||||
/// Prevents the letter/whatever entered to show up in the TextBox
|
/// Prevents the letter/whatever entered to show up in the TextBox
|
||||||
/// Without this, a "A" key press would appear as "aControl, Alt + A".
|
/// Without this, a "A" key press would appear as "aControl, Alt + A".
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void HandlePreviewTextInput(object sender, TextCompositionEventArgs e)
|
private void HandlePreviewTextInput(object sender, TextCompositionEventArgs e) => e.Handled = true;
|
||||||
{
|
|
||||||
e.Handled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if TODO // HOTKEY
|
#if TODO // HOTKEY
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -504,9 +504,7 @@ namespace SystemTrayMenu.UserInterface
|
||||||
|
|
||||||
Settings.Default.CheckForUpdates = checkBoxCheckForUpdates.IsChecked ?? false;
|
Settings.Default.CheckForUpdates = checkBoxCheckForUpdates.IsChecked ?? false;
|
||||||
|
|
||||||
#if TODO // HOTKEY
|
Settings.Default.HotKey = textBoxHotkey.HotkeyFunction?.GetHotkeyString() ?? string.Empty;
|
||||||
Settings.Default.HotKey = new KeysConverter().ConvertToInvariantString(textBoxHotkey.Hotkey | textBoxHotkey.HotkeyModifiers);
|
|
||||||
#endif
|
|
||||||
Settings.Default.CurrentCultureInfoName = comboBoxLanguage.SelectedValue.ToString();
|
Settings.Default.CurrentCultureInfoName = comboBoxLanguage.SelectedValue.ToString();
|
||||||
Settings.Default.SizeInPercent = numericUpDownSizeInPercent.Value;
|
Settings.Default.SizeInPercent = numericUpDownSizeInPercent.Value;
|
||||||
Settings.Default.IconSizeInPercent = numericUpDownIconSizeInPercent.Value;
|
Settings.Default.IconSizeInPercent = numericUpDownIconSizeInPercent.Value;
|
||||||
|
|
Loading…
Reference in a new issue