autohotkeysleep-mode

Best way for AutoHotkey v2 to send `Win+x`, `u`, `s` sequence


With AutoHotkey v2 I'm trying to put my computer to sleep using the sequence Win+x (to bring up the quick links menu), u (for "Shutdown…"), s (for "Sleep"). (If you know a better way to put the computer to sleep let me know, but this has been discussed extensively in other questions, and the recommended approach puts the computer into hibernate on my machine, not sleep, as explained elsewhere. So let's just stick with this scenario.)

Let's say I want to map this sequence to Win+z (for "zzzzzzz" for sleeping). This does not work:

#z::
{
  SendEvent "#xus"
}

This does not work either:

#z::
{
  SendEvent "#x"
  SendEvent "u"
  SendEvent "s"
}

This is the only approach that seems to work, sort of:

#z::
{
  SendEvent "#x u s"
}

It seems sort of finicky and doesn't always work. Sometimes it makes a system sound, as if an incorrect key was pressed. Moreover I don't know what the spaces mean: is AutoHotkey sending spaces, or is that merely a separator between keys to send? I used it by trial-and-error, and could find no documentation on what spaces even mean for AutoHotkey v2.

What's the best way to send the Win+x, u, s sequence in AutoHotkey v2 to achieve my goal of putting the computer to sleep via a "sleep" menu? (If there is a better, more easily achievable "sleep" menu I'd be interesting in knowing that as well.)


Solution

  • Dieisson's suggestion to add a Sleep was helpful, but there are more details, including one tip I found at https://stackoverflow.com/a/71753607 . Using AutoHotkey v2, here is the best I've found so far.

    #z::
    {
      WinActivate "ahk_class Shell_TrayWnd"
      Send "#x"
      Sleep 100
      Send "us"
    }