20
20
using System . Threading ;
21
21
using System . Threading . Tasks ;
22
22
using System . Windows . Forms ;
23
+ using Microsoft . Win32 ;
23
24
using Serilog ;
24
25
using SoundSwitch . Audio . Manager ;
25
26
using SoundSwitch . Audio . Manager . Interop . Com . User ;
@@ -122,6 +123,8 @@ private static void RunForm()
122
123
AppDomain . CurrentDomain . UnhandledException += ( sender , args ) => _exceptionEventHandler ( sender , new ThreadExceptionEventArgs ( ( Exception ) args . ExceptionObject ) ) ;
123
124
}
124
125
126
+ SystemEvents . PowerModeChanged += SystemEventsOnPowerModeChanged ;
127
+
125
128
_instance = new WindowsAPIAdapter ( ) ;
126
129
_instance . CreateHandle ( ) ;
127
130
_instance . _msgNotifyShell = Interop . RegisterWindowMessage ( "SHELLHOOK" ) ;
@@ -131,9 +134,26 @@ private static void RunForm()
131
134
Log . Information ( "End of the WindowsAPIAdapter thread" ) ;
132
135
}
133
136
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
+
134
153
private void EndForm ( )
135
154
{
136
155
Close ( ) ;
156
+ SystemEvents . PowerModeChanged -= SystemEventsOnPowerModeChanged ;
137
157
}
138
158
139
159
protected override void Dispose ( bool disposing )
0 commit comments