Make HotkeyControl buildable again.

Also try registering the hotkey on startup.
Extended error message why registering of a hotkey failed.
This commit is contained in:
Peter Kirmeier 2023-05-21 21:13:18 +02:00
parent d263eb4588
commit f8c9913933
6 changed files with 614 additions and 647 deletions

View file

@ -6,9 +6,8 @@ namespace SystemTrayMenu.Helpers
{
using System;
using System.Windows.Input;
#if TODO //HOTKEY
using SystemTrayMenu.DllImports;
using SystemTrayMenu.UserInterface.HotkeyTextboxControl;
#endif
using SystemTrayMenu.Utilities;
using static SystemTrayMenu.Utilities.FormsExtensions;
@ -81,12 +80,8 @@ namespace SystemTrayMenu.Helpers
modifiers |= KeyboardHookModifierKeys.Win;
}
}
#if TODO //HOTKEY
RegisterHotKey(
modifiers,
HotkeyControl.HotkeyFromString(
Properties.Settings.Default.HotKey));
#endif
RegisterHotKey(modifiers, HotkeyControl.HotkeyFromString(Properties.Settings.Default.HotKey));
}
/// <summary>
@ -118,11 +113,10 @@ namespace SystemTrayMenu.Helpers
{
currentId += 1;
if (!DllImports.NativeMethods.User32RegisterHotKey(
window.Handle, currentId, modifier, (uint)key))
if (!NativeMethods.User32RegisterHotKey(window.Handle, currentId, modifier, (uint)key))
{
throw new InvalidOperationException(
Translator.GetText("Could not register the hot key."));
string errorHint = NativeMethods.GetLastErrorHint();
throw new InvalidOperationException(Translator.GetText("Could not register the hot key.") + " (" + errorHint + ")");
}
}

View file

@ -14,6 +14,18 @@ namespace SystemTrayMenu.DllImports
/// </summary>
public static partial class NativeMethods
{
internal static string GetLastErrorHint()
{
const int ERROR_HOTKEY_ALREADY_REGISTERED = 1409;
int error = Marshal.GetLastWin32Error();
return error switch
{
ERROR_HOTKEY_ALREADY_REGISTERED => "ERROR_HOTKEY_ALREADY_REGISTERED",
_ => error.ToString(),
};
}
[SupportedOSPlatform("windows")]
[DllImport("user32.dll", EntryPoint = "RegisterHotKey", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]

View file

@ -104,9 +104,6 @@
<Optimize>True</Optimize>
<NoWarn>1701;1702;WFAC010;MSB3061</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Remove="UserInterface\HotkeyTextboxControl\HotkeyControl.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Remove="UserInterface\Menu.resx" />
</ItemGroup>
@ -145,7 +142,6 @@
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="UserInterface\HotkeyTextboxControl\HotkeyControl.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Resources\SystemTrayMenu.ico" />

File diff suppressed because it is too large Load diff

View file

@ -11,6 +11,7 @@ namespace SystemTrayMenu.UserInterface
using System.Runtime.Versioning;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using SystemTrayMenu.Properties;
@ -50,6 +51,8 @@ namespace SystemTrayMenu.UserInterface
Icon = imageSource;
}
}
PreviewKeyDown += HandlePreviewKeyDown;
#if TODO // HOTKEY
// Initialize and replace here here, because designer always removes it
InitializeTextBoxHotkeyAndReplacetextBoxHotkeyPlaceholder();
@ -411,28 +414,6 @@ namespace SystemTrayMenu.UserInterface
}
#if TODO // HOTKEY
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Escape:
if (!inHotkey)
{
DialogResult = DialogResult.Cancel;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
break;
default:
return base.ProcessCmdKey(ref msg, keyData);
}
return true;
}
/// <summary>
/// Helper method to cleanly register a hotkey.
/// </summary>
@ -596,6 +577,27 @@ namespace SystemTrayMenu.UserInterface
return useStartupTask;
}
private void HandlePreviewKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Escape:
#if TODO // HOTKEY
if (!inHotkey)
{
DialogResult = DialogResult.Cancel;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
#endif
break;
default:
break;
}
}
private void ButtonOk_Click(object sender, RoutedEventArgs e)
{
if (checkBoxSetFolderByWindowsContextMenu.IsChecked ?? false)

View file

@ -7,9 +7,8 @@ namespace SystemTrayMenu.Utilities
using System;
using System.Diagnostics;
using System.Linq;
#if TODO //HOTKEY
using System.Windows.Input;
using SystemTrayMenu.UserInterface.HotkeyTextboxControl;
#endif
internal static class SingleAppInstance
{
@ -25,12 +24,13 @@ namespace SystemTrayMenu.Utilities
{
if (Properties.Settings.Default.SendHotkeyInsteadKillOtherInstances)
{
#if TODO // HOTKEY
Key modifiers = HotkeyControl.HotkeyModifiersFromString(Properties.Settings.Default.HotKey);
ModifierKeys modifiers = HotkeyControl.HotkeyModifiersFromString(Properties.Settings.Default.HotKey);
Key hotkey = HotkeyControl.HotkeyFromString(Properties.Settings.Default.HotKey);
try
{
#if TODO // HOTKEY - Maybe replace with sockets or pipes?
// E.g. https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-use-named-pipes-for-network-interprocess-communication
List<VirtualKeyCode> virtualKeyCodesModifiers = new();
foreach (string key in modifiers.ToString().ToUpperInvariant().Split(", "))
{
@ -58,15 +58,14 @@ namespace SystemTrayMenu.Utilities
new InputSimulator().Keyboard.ModifiedKeyStroke(virtualKeyCodesModifiers, virtualKeyCodeHotkey);
success = false;
#endif
}
catch (Exception ex)
{
Log.Warn($"Send hoktey {Properties.Settings.Default.HotKey} to other instance failed", ex);
}
#endif
}
if (!Properties.Settings.Default.SendHotkeyInsteadKillOtherInstances)
else
{
try
{