Putting PC into sleep mode programmatically

3.5k views Asked by At

An old problem, maybe (?) resolved for older Windows version, but not for Windows 10!

The solutions proposed in old discussions (... SetSuspendState with parameters 0,1,0 (or Sleep), with or without prior hibernate off ...) dont work on Windows 10 systems; at least not on all: the initiated mode is mostly hibernate, not sleep.

What I found out: In older Windows versions there was something like this in the Registry: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\sleep\command] @="rundll32.exe powrprof.dll,SetSuspendState Sleep" This obviously was the sleep command. At least on my Windows 10 PCs there is no such entry!

Somebody here with new answers?

================== EDITED: After this my OP I discovered that this is not a Windows 10 issue, it is a Surface 3 issue, related to the power option used on Surface 3 as Sleep mode:

The Sleep mode used by Surface 3 seems to be "Modern Standby" = "S0 Low Power Idle" = "Connected Network Standby". (Not the usual S3 Sleep mode!!!)

The commands using SetSuspendState seems to be unable at all to initiate the Sleep mode on a Surface 3.

A new wording for my question: All what I would like to have is a command which does the same as the Power button on a Surface 3 which is set to "power-button action = Sleep", or as the Sleep button on an external (USB) keyboard when Surface 3 is set to "sleep-button action = Sleep".

2

There are 2 answers

3
sfeng On

I haven't found a satisfactory answer to this question on the internet, my current solution is simulating key pressing.

In Python, I use the pyautogui module. Here's my codes:

import pyautogui

pyautogui.press("win")
pyautogui.press("tab")
pyautogui.press("tab")
pyautogui.press("tab")
pyautogui.press("enter")
pyautogui.press("tab")
pyautogui.press("enter")
0
newbieforever On

I found this new solution on Programmatically enter Connected Standby in C#

On my Surface 3, I am able now to activate the Sleep mode ("Modern Standby" = "S0 Low Power Idle" = "Connected Network Standby") by a command which turns off the monitor. In Autohotkey this is simply the line:

SendMessage, 0x112, 0xF170, 2,, Program Manager

I can see in the sleepstudy-report.html (created by powercfg /sleepstudy) that really "Modern Standby" is entered by this.