Skip to content

Commit

Permalink
fix(Hotkey): Fix hotkey not being registered when computer comes back…
Browse files Browse the repository at this point in the history
… from sleep

Fixes #1041
Fixes #997
  • Loading branch information
Belphemur committed Nov 18, 2022
1 parent 6fa692a commit ed05d62
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions SoundSwitch/Framework/WinApi/WindowsAPIAdapter.cs
Expand Up @@ -20,6 +20,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using Serilog;
using SoundSwitch.Audio.Manager;
using SoundSwitch.Audio.Manager.Interop.Com.User;
Expand Down Expand Up @@ -122,6 +123,8 @@ private static void RunForm()
AppDomain.CurrentDomain.UnhandledException += (sender, args) => _exceptionEventHandler(sender, new ThreadExceptionEventArgs((Exception)args.ExceptionObject));
}

SystemEvents.PowerModeChanged += SystemEventsOnPowerModeChanged;

_instance = new WindowsAPIAdapter();
_instance.CreateHandle();
_instance._msgNotifyShell = Interop.RegisterWindowMessage("SHELLHOOK");
Expand All @@ -131,9 +134,26 @@ private static void RunForm()
Log.Information("End of the WindowsAPIAdapter thread");
}

private static void SystemEventsOnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if (e.Mode != PowerModes.Resume)
{
return;
}
Log.Information("Computer coming back from sleep, re-registering hotkeys");

foreach (var (hotKey, hotKeyId) in _instance._registeredHotkeys)
{
var wasRegistered = NativeMethods.UnregisterHotKey(_instance.Handle, hotKeyId);
var isRegistered = NativeMethods.RegisterHotKey(_instance.Handle, hotKeyId, (uint)hotKey.Modifier, (uint)hotKey.Keys);
Log.Information("Re-registering hotkey {Hotkey}: Result({WasRegistered}, {IsRegistered})", hotKey, wasRegistered, isRegistered);
}
}

private void EndForm()
{
Close();
SystemEvents.PowerModeChanged -= SystemEventsOnPowerModeChanged;
}

protected override void Dispose(bool disposing)
Expand Down

0 comments on commit ed05d62

Please sign in to comment.