Documentation's sleep/suspend script puts laptop into hibernate mode for Windows 10/11 with modern standby Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
takanuva15
Posts: 24
Joined: 22 Nov 2019, 23:05
Location: USA
Contact:

Documentation's sleep/suspend script puts laptop into hibernate mode for Windows 10/11 with modern standby

Post by takanuva15 » 30 Jan 2022, 11:25

Hi, I'm trying to write a script to put my new laptop into sleep mode with Windows+S, but when I follow the "suspend" script in the documentation, it puts the laptop into hibernate, not sleep:

Code: Select all

#s::DllCall("PowrProf\SetSuspendState", "Int", 0, "Int", 0, "Int", 0)
I should note that on my old laptop from 2016, this script works fine and puts the laptop into sleep mode. However, on my new 2020 laptop with Windows "S0 modern standby", the script puts the computer into hibernate mode instead of sleep. If you have a recent Windows laptop (eg a laptop released in last 2-3 years, or an "Intel Evo" laptop, etc.), then you should be able to replicate the problem with the above script. I have tested the above script with 2 laptops, a 2020 MSI Prestige laptop running Windows 10, and a 2020 HP laptop running Windows 11, and it has the same issue with both - it puts the laptop into hibernate, not sleep. I am able to tell the difference because of the significantly longer boot-up time and the power button not flashing on/off as it does when the computer is in sleep mode. If you want me to pull logs from Event Viewer, etc. just let me know.)

Important note: "S0 Modern standby" is a new type of sleep that Windows has installed into recent laptops to make them wake faster from sleep mode (in a manner similar to smartphones). My old laptop has "S3 sleep" which is how the old Windows sleep used to work (which I assume is why the script works with my older laptop). You can read about the difference between these 2 sleep modes in this recent Microsoft article from 03/2021. You can verify what sleep mode your laptop supports by running powercfg /a in a cmd window.

I am not versed enough with AutoHotkey or the Windows API to know what's the right dllcall/argument to send to put Windows into "modern standby" sleep. Can someone point me in the right direction for how to adjust the above sleep script to put my new laptop into sleep mode and not hibernate?

Specs of the HP laptop I am testing the script on: Edit: Based on some research, it seems that "SetSuspendState" may not work in general for putting modern laptops into sleep mode. See this forum topic: https://social.msdn.microsoft.com/Forum ... =vcgeneral

In addition, someone posted a working script here, but it requires additional beeps:

Code: Select all

; {{Win+0 --> Sleep
#0::
SoundBeep
SoundBeep
SendMessage, 0x112, 0xF170, 2,, Program Manager
SoundBeep
return
Last edited by takanuva15 on 30 Jan 2022, 13:43, edited 1 time in total.

Currently running AutoHotkey v1.1.33.10 on a Windows 10 machine. (Also testing v2 with v2.0-a133)
All code I post on the AutoHotkey forum is under the MIT License unless otherwise noted in the post. (Basically, it's free for anyone to use/modify, but please credit me if you decide to use it as part of another post or project that will be public.)
"The tomorrow of yesterday is today, so don't make an excuse to delay!"
User avatar
mikeyww
Posts: 27214
Joined: 09 Sep 2014, 18:38

Re: Documentation's sleep/suspend script puts laptop into hibernate mode for Windows 10/11 with modern standby

Post by mikeyww » 30 Jan 2022, 11:40

One option is to disable hibernation before your sleep command. That can be done by command line via powercfg. Another option is below.

Code: Select all

shutdownapp = d:\utils\PSTools\psshutdown64.exe
SUSPEND     = -d ; https://docs.microsoft.com/en-us/sysinternals/downloads/psshutdown
COUNTDOWN   = -t
Run, %shutdownapp% %SUSPEND% %COUNTDOWN% 0 -accepteula
User avatar
takanuva15
Posts: 24
Joined: 22 Nov 2019, 23:05
Location: USA
Contact:

Re: Documentation's sleep/suspend script puts laptop into hibernate mode for Windows 10/11 with modern standby

Post by takanuva15 » 30 Jan 2022, 13:38

mikeyww wrote:
30 Jan 2022, 11:40
One option is to disable hibernation before your sleep command. That can be done by command line via powercfg. Another option is below.
Hi, thanks for your reply! I do want the hibernate option enabled because I always hibernate my laptop overnight so it doesn't use unnecessary power. However, I disabled the hibernate option for testing your script but it still puts my laptop into hibernate mode. After reading your script, it looks like you are basically running this command in cmd:
psshutdown64 -d -t 0 -accepteula

When I download the PSUtils and run the above command directly in cmd, it also puts my laptop into hibernate mode. For the sake of completeness, I also tried this script after running AutoHotkey in administrator mode but it also causes the laptop to go into hibernate:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#s::
shutdownapp = C:\Users\hp-user\Downloads\PSTools\psshutdown64.exe
SUSPEND     = -d ; https://docs.microsoft.com/en-us/sysinternals/downloads/psshutdown
COUNTDOWN   = -t
Run, powercfg /h off
Run, %shutdownapp% %SUSPEND% %COUNTDOWN% 0 -accepteula
Run, powercfg /h on
I did some research and it turns out you can view what "sleep modes" your system allows by running powercfg /a in a cmd window. For me, it shows the following output:

Code: Select all

C:\windows\system32>powercfg /a
The following sleep states are available on this system:
    Standby (S0 Low Power Idle) Network Connected
    Hibernate
    Fast Startup
 
The following sleep states are not available on this system:
    Standby (S1)
        The system firmware does not support this standby state.
        This standby state is disabled when S0 low power idle is supported.
 
    Standby (S2)
        The system firmware does not support this standby state.
        This standby state is disabled when S0 low power idle is supported.
 
    Standby (S3)
        The system firmware does not support this standby state.
        This standby state is disabled when S0 low power idle is supported.
 
    Hybrid Sleep
        Standby (S3) is not available.
@mikeyww I'm not sure if you tested your script on a Windows machine with the "S0 Modern Standby" feature, but try running the powercfg /a command to see if your laptop has it. If so, I'd imagine your laptop will also go into hibernate after running the above psshutdown64 command since I have not done any special customization to my laptop's power plan at all.

The above notwithstanding though, after doing some additional research, it turns out someone had a similar problem with their Microsoft Surface tablet and someone else posted a script which actually works to put my new Windows laptop into sleep mode with modern standby:
LorenAmelang wrote:
31 Oct 2021, 21:27
Confused and Amazed - this actually works:

Code: Select all

; {{Win+0 --> Sleep
#0::
SoundBeep
SoundBeep
SendMessage, 0x112, 0xF170, 2,, Program Manager
SoundBeep
return
However, I don't like the fact that these random beeps are required to make the script work - would anyone know an alternative to get the laptop to go into sleep silently?
(I have tried running just the SendMessage command, but that only flashes the laptop screen off for a moment before the lockscreen just reappears without the laptop entering sleep mode, as mentioned in the other post I linked with the above solution)

Currently running AutoHotkey v1.1.33.10 on a Windows 10 machine. (Also testing v2 with v2.0-a133)
All code I post on the AutoHotkey forum is under the MIT License unless otherwise noted in the post. (Basically, it's free for anyone to use/modify, but please credit me if you decide to use it as part of another post or project that will be public.)
"The tomorrow of yesterday is today, so don't make an excuse to delay!"
User avatar
mikeyww
Posts: 27214
Joined: 09 Sep 2014, 18:38

Re: Documentation's sleep/suspend script puts laptop into hibernate mode for Windows 10/11 with modern standby

Post by mikeyww » 30 Jan 2022, 13:45

Hmm. Thanks for the details. I do have standby as well as hibernation, and the PsShutdown worked for sleep, at least as far as I could tell. I am not on a Surface tablet but a laptop computer. I do not know the answer about the SendMessage; others here may know more.

It appears that you are saying that your tablet hibernated even after you disabled hibernation. This suggests that your disabling action failed. You could double-check whether you are actually able to disable your hibernation. It might require admin mode in your setting. How did you verify that hibernation was successfully disabled?
User avatar
takanuva15
Posts: 24
Joined: 22 Nov 2019, 23:05
Location: USA
Contact:

Re: Documentation's sleep/suspend script puts laptop into hibernate mode for Windows 10/11 with modern standby  Topic is solved

Post by takanuva15 » 30 Jan 2022, 21:17

@mikeyww I checked hibernation was disabled by opening the start menu and clicking the power button to see if the hibernate option was available.

Ok, after an hour or two of testing different key combos and exploring the documentation, I finally got something that works 100% of the time:

Code: Select all

#s Up::
Sleep 500
SendMessage, 0x112, 0xF170, 2,, Program Manager
return
Explanation: Basically, the SoundBeeps that were present in the previous working script were just serving as delays to give the user enough time to release the hotkeys before the actual SendMessage command went through. (You could actually replace all the SoundBeeps with just a single SoundBeep, 37, 300 before the SendMessage call and the previous script would still work). The problem with not including the Soundbeeps is that as soon as you pressed the hotkey, the SendMessage call would execute. Then, within a few ms, you would take your fingers off the keyboard to release the hotkey and those "key releases" would register as key-up events in Windows, and thus Windows would immediately cancel trying to go into sleep mode.

I just adjusted the script to only run after the s key is released, by then which any normal person would be able to release the Windows key as well within 500 ms. After that the SendMessage goes through and the laptop goes into sleep mode smoothly. You could technically reduce the sleep time to 300 if you press and release hotkeys immediately, but there's no harm in making the script more robust by giving 500 ms to release the hotkey. (addendum: The reason we still need the Sleep 500 even though we have the s Up is that you could potentially still hold down the windows key after releasing the s key, so extra time is required to be sure both keys are released before executing the sleep command)

The only final issue I have with this script (and this new version of Windows modern standby in general) is that if you accidentally wiggle the mouse within 1-2 s of executing the sleep command, Windows cancels trying to go into sleep mode. This is bad UX since it's easy to vibrate the desk slightly while standing up after putting the laptop into sleep mode (especially with high dpi mice like mine's), so I have to practically slide away from my desk like a ninja (or sit there and do nothing for 2-3 s) after putting the laptop to sleep so that I don't accidentally shake the mouse and cancel sleep. (Executing something like BlockInput, MouseMove does not help since it seems to not impact the mouse-movement detection as the computer goes to sleep. I have already checked the Power Management tab for all HID-compliant mouse devices, and I've also found that that setting only "kicks in" after 4 seconds of the laptop going into sleep mode - you will see the laser pointer go off on the mouse. Before then though, any mouse shake will still cancel sleep mode). Since this new issue is outside the scope of this particular topic, I will not pursue it further here - I will make a new topic if I can't figure out how to perhaps disconnect the mouse before running the sleep command so that a mouse wiggle can't cancel the sleep command.

Currently running AutoHotkey v1.1.33.10 on a Windows 10 machine. (Also testing v2 with v2.0-a133)
All code I post on the AutoHotkey forum is under the MIT License unless otherwise noted in the post. (Basically, it's free for anyone to use/modify, but please credit me if you decide to use it as part of another post or project that will be public.)
"The tomorrow of yesterday is today, so don't make an excuse to delay!"
User avatar
mikeyww
Posts: 27214
Joined: 09 Sep 2014, 18:38

Re: Documentation's sleep/suspend script puts laptop into hibernate mode for Windows 10/11 with modern standby

Post by mikeyww » 31 Jan 2022, 05:53

Thanks for the solution! I'm still not sure how you could hibernate your computer if hibernation is disabled, but at least you have something here that works.
User avatar
WAZAAAAA
Posts: 89
Joined: 13 Jan 2015, 19:48

Re: Documentation's sleep/suspend script puts laptop into hibernate mode for Windows 10/11 with modern standby

Post by WAZAAAAA » 14 May 2024, 09:48

even better than using fixed sleep times: by using KeyWait you can make it so the code reliably starts upon RELEASE of both keys

Code: Select all

LWin & S up:: ;screen OFF
KeyWait, LWin
SendMessage,0x112,0xF170,2,,Program Manager
return
(but that code only turns off the screen... unless we want to consider the travesty that is S0 on modern laptops a real standby)
YOU'RE NOT ALEXANDER
Post Reply

Return to “Ask for Help (v1)”