Last active
September 16, 2023 02:36
-
-
Save niski84/22043ba2bcee79e0ce32c0cb26fa9ade to your computer and use it in GitHub Desktop.
detect screen saver running on windows package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Package screensaver provides functions for detecting screensaver status on Windows. | |
package screensaver | |
import ( | |
"syscall" | |
"unsafe" | |
) | |
// import user32.dll | |
var ( | |
user32 = syscall.NewLazyDLL("user32.dll") | |
systemParametersInfo = user32.NewProc("SystemParametersInfoW") | |
) | |
// IsActive checks if the screensaver is active in Windows. | |
func IsActive() bool { | |
var running int32 | |
systemParametersInfo.Call(uintptr(0x0072), uintptr(0), uintptr(unsafe.Pointer(&running)), uintptr(0)) // 0x0072 is SPI_GETSCREENSAVERRUNNING | |
// if running is not 0, then screensaver is active | |
return running != 0 | |
} | |
func main() { | |
for { | |
// check if screensaver is active | |
if screensaver.IsActive() { | |
fmt.Println("Screensaver is active. Sleeping for 5 minutes.") | |
// sleep for 5 minutes | |
time.Sleep(5 * time.Minute) | |
} else { | |
fmt.Println("Screensaver is not active. Resuming.") | |
// Do whatever you want to do when screensaver is not active. | |
// ... | |
// For demonstration, break out of the loop | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment