Programmatically reset Windows XP IDLE time

2019-04-28 05:35发布

问题:

Windows resets the IDLE time every time the user touches the keyboard or the mouse. My application needs to reset the IDLE time at specific moments, but i can't figure out how to do this programmatically.

The following does NOT reset the IDLE time using VB, C# or QT4.
- Programmatic mouse movement / click.
- Programmatic keystroke.
Somehow Windows knows these actions are simulated.

How can i reset the IDLE time? Any thoughts will be greatly appreciated!

回答1:

Use SetThreadExecutionState(). The ES_SYSTEM_REQUIRED option (2) resets the system idle timer. Visit pinvoke.net for the required declarations.



回答2:

If you need the display to stay on for example, you would call SetThreadExecutionState(ES_DISPLAY_REQUIRED|ES_CONTINUOUS);

On 2000/XP you could use the ES_USER_PRESENT flag (Does not work on Vista+)

To disable the screen saver, you can handle WM_SYSCOMMAND's SC_SCREENSAVE (You must be the foreground window, otherwise use SystemParametersInfo)



回答3:

In principle SetThreadExecutionState() should do what you need. But I have found it not to work with the system required flag in some situations on some modern Windows. Sorry, I can't remember exact details.

As a fallback you can just fake some input by calling SendInput() with a null mouse move message for example. My guess is that your input faking failed because you called SendMessage or PostMessage.