Customize a macOS Keyboard Shortcut for One-Click Sleep

2019/08/10

Preface

The coolest thing about using an MBP is being able to just close the lid and walk away, then open it and get right back to work. But what about when you’re using an external monitor? I’m more used to hitting a shortcut to turn off the screen and then leaving—when I need it again, I just tap any key on the keyboard. Thanks to macOS’s excellent sleep management, it wakes up instantly and gets me back into “work mode” (you know what I mean). Sure, there’s a Sleep button you can click, but how is that ever as convenient as a keyboard shortcut? Reference 1 is a Zhihu discussion about this issue—hundreds of answers, and basically none of them provide a truly practical solution.

Below are four methods I’ve summarized that can achieve sleep via a keyboard shortcut.

Tutorial

Method 1 - Use the system one-click lock screen

First, macOS provides a built-in shortcut for one-click lock screen (ctrl+comd+Q).

image-20201011164249647

Similar to Win + L on Windows: press it and it locks the screen, then after a few seconds it automatically goes to sleep. But since this shortcut is provided by the system, you can’t customize it. Also, after locking it doesn’t go black immediately—there’s a few seconds where the screen stays on. Personally, I’m not used to that. (And honestly, if they provide a shortcut for “Lock Screen,” why doesn’t the “Sleep” option in the same menu get one too?)

Method 2 - Use a special keyboard

If you’re using a MiaoControl keyboard or an older MBP, you can find the key marked with the red-circled 3 (the eject key). Use the key combo shown in the image: ctrl+comd+eject to sleep. But in reality, not every keyboard has this key—so if you have it, great, it’s pretty convenient.

image-20201011162357934

Method 3 - Use third-party software

You can also do it with third-party software. The more complex option is the BAB app mentioned in Reference 2: its keyboard module can set up one-key sleep. But the software is too powerful—not only can it set sleep, it can configure all kinds of system actions, which makes it very easy to conflict with other apps. I used it for a while but couldn’t accept the impact of those conflicts, so I gave up on it.image-20201011182220164

A simpler one is an app called QuickSleep (download link from PC6 in Reference 3). The whole app provides only one feature: lock screen. You can assign any shortcut you want—super simple.

image-20201011182327162

I used to love tinkering and downloading all kinds of software, but once you overdo it, the system starts running into all sorts of issues. Later I realized it was conflicts between apps. These days, if I can achieve something without third-party software, I’ll try my best not to install anything extra. Automator is a built-in macOS tool for running scripts. Since it’s built in, there’s nothing to download. The idea is simple: macOS can run shell scripts, and we can use Automator to execute a sleep script we write ourselves—literally a one-liner. Here are the steps:

  • Search for Automator in Spotlight and open itimage-20201011183821301

  • In the Automator dialog, choose Quick Action and click Chooseimage-20201011183936701

  • In the new dialog that pops up, follow the image to select UtilitiesRun Shell Script. Make sure the “Workflow receives” option marked as 3 is set to “no input”. Then enter the code below at the position marked 4 and click Save (if you just quit, it will prompt you to save).

    pmset sleepnow
    

    image-20201011184713686

  • Open System PreferencesKeyboardShortcutsServices, and find the script you just named (usually under General—it’s definitely there, just look carefully)

    image-20201011185015435

  • Once you find it, assign a shortcut that doesn’t conflict with the system, and you’re doneimage-20201011185152126

Give it a try? It works really well, and you don’t need any third-party tools. I personally got used to using ctrl+commd+L to turn off the screen—what about you?

References

  1. Zhihu - How can macOS lock the screen with a keyboard shortcut?
  2. BAB Official Site
  3. PC6 - QuickSleep Download

All articles in this blog, unless otherwise stated, are licensed under @Oreoft . Please indicate the source when reprinting!

Table of Contents