Skip to content

Commit ed05d62

Browse files
committed
fix(Hotkey): Fix hotkey not being registered when computer comes back from sleep
Fixes #1041 Fixes #997
1 parent 6fa692a commit ed05d62

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

SoundSwitch/Framework/WinApi/WindowsAPIAdapter.cs

+20
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Threading;
2121
using System.Threading.Tasks;
2222
using System.Windows.Forms;
23+
using Microsoft.Win32;
2324
using Serilog;
2425
using SoundSwitch.Audio.Manager;
2526
using SoundSwitch.Audio.Manager.Interop.Com.User;
@@ -122,6 +123,8 @@ private static void RunForm()
122123
AppDomain.CurrentDomain.UnhandledException += (sender, args) => _exceptionEventHandler(sender, new ThreadExceptionEventArgs((Exception)args.ExceptionObject));
123124
}
124125

126+
SystemEvents.PowerModeChanged += SystemEventsOnPowerModeChanged;
127+
125128
_instance = new WindowsAPIAdapter();
126129
_instance.CreateHandle();
127130
_instance._msgNotifyShell = Interop.RegisterWindowMessage("SHELLHOOK");
@@ -131,9 +134,26 @@ private static void RunForm()
131134
Log.Information("End of the WindowsAPIAdapter thread");
132135
}
133136

137+
private static void SystemEventsOnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
138+
{
139+
if (e.Mode != PowerModes.Resume)
140+
{
141+
return;
142+
}
143+
Log.Information("Computer coming back from sleep, re-registering hotkeys");
144+
145+
foreach (var (hotKey, hotKeyId) in _instance._registeredHotkeys)
146+
{
147+
var wasRegistered = NativeMethods.UnregisterHotKey(_instance.Handle, hotKeyId);
148+
var isRegistered = NativeMethods.RegisterHotKey(_instance.Handle, hotKeyId, (uint)hotKey.Modifier, (uint)hotKey.Keys);
149+
Log.Information("Re-registering hotkey {Hotkey}: Result({WasRegistered}, {IsRegistered})", hotKey, wasRegistered, isRegistered);
150+
}
151+
}
152+
134153
private void EndForm()
135154
{
136155
Close();
156+
SystemEvents.PowerModeChanged -= SystemEventsOnPowerModeChanged;
137157
}
138158

139159
protected override void Dispose(bool disposing)

0 commit comments

Comments
 (0)