I wanted to be able to detect when Dark mode was turned on or off in my Go program so I can swap icons in my tray app on Windows so I wrote this.
When Dark Mode is turned on or off, it writes to a pair of registry keys, both of which Windows allows you to set independently in Settings > Personalization > Colors.
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize
SystemUsesLightTheme DWORD 0 or 1 // Changes the taskbar and system tray color
AppsUseLightTheme DWORD 0 or 1 // Changes app colors
Mine is a tray app, so I am monitoring the first one. I did not want to just periodically poll the registry key and I wanted to be able to react immediately when the registry key is changed, so I am using the win32 function RegNotifyChangeKeyValue in advapi32.dll to tell the OS to notify my app so I can swap out the icon for a color-appropriate one.
I start the monitor function in main, which loads the RegNotifyChangeKeyValue function from advapi32 and starts a goroutine that calls that function with a pointer to the registry key I want to monitor. When it detects a change, the registry value is read and if it is actually different, a function that was passed to the monitor function is called with a boolean representing the current state of dark mode.
Awesome thank you!