Jump to content
Sign in to follow this  
dummzeuch

Blocking the Windows Screen Saver in Delphi

Recommended Posts

Sometimes your program needs to block the screen saver from automatically kicking in. My use case was that the program was recording data and whenever the screen saver was active, the data was lost (No idea why, it probably had something to do with the way HID is implemented in Windows.)
So I was looking for a way to fix that without forcing the user to turn off the screen saver. The methods that used to work under Windows XP no longer work in Windows 7 and later (I don’t care about Vista), so I googled and found this question on StackOverflow. The Windows API functions PowerCreateRequest + PowerSetRequest mentioned in the highest voted answer looked promising. Unfortunately they don’t seem bo be available in Delphi

read on in my blog post

 

  • Like 3
  • Thanks 1

Share this post


Link to post

The code is now in dzlib (see blog post for links).

  • Like 1
  • Thanks 1

Share this post


Link to post

Our laptops are under company governance, so we can't prevent the session from locking as it is dictated by the policies.  In addition, the idle time is really short.

So - we wrote LockBlock which basically simulates toggling the numlock every 60 seconds.  That prevented the auto locking, but it also prevented the screensaver from kicking in when the dektkop was locked, so I added handling of lock/unlock to disable the fake keystrokes when it was locked.

Super simple UI: 60 second interval, starting at 8 in the morning, lasting for 8 hours - with an add an extra hour button for off work hours lock blocking.

 

image.thumb.png.8d81afd5e92eefe56b17e83172bbe364.png

 

Project source attached.  

LockBlock.zip

Share this post


Link to post

I used a similar hack in an earlier version of my program: Wiggling the mouse pointer. While that worked fine under Windows XP, it doesn't under Windows 10 (no idea about Windows 7 and 8). That was the reason I researched how to do it "right".

 

Toggling numlock all the time would drive me crazy.

Share this post


Link to post

I use this function for this:

https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-setthreadexecutionstate

Share this post


Link to post
2 hours ago, Lykke said:

setthreadexecutionstate

Yes, that is what I used but it doesn't take care of the screen saver.
Thanks to this blog post I updated to use PowerCfg when (OS>=Win7).

 

Share this post


Link to post
On 6/14/2019 at 5:09 PM, dummzeuch said:

I used a similar hack in an earlier version of my program: Wiggling the mouse pointer. While that worked fine under Windows XP, it doesn't under Windows 10 (no idea about Windows 7 and 8). That was the reason I researched how to do it "right".

 

Toggling numlock all the time would drive me crazy.

It turns NumLock off/on/off (or on/off/on) within less than 30 ms every 60 seconds, so I don't think it will bother you much. 

Share this post


Link to post
12 minutes ago, Lars Fosdal said:

NumLock off/on/off

Sounds like an 'Awesome' feature while entering financial data into a spreadsheet :classic_laugh:

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×