I am looking for a quick way to sleep without closing the lid, or using the mouse.
If it matters, I have Lion version OS and a MacBook Air.
If others do have a keyboard with an eject key, you can just press Command ⌘+Option ⌥+Eject ⏏.
Control ^+Shift ⇧+Eject ⏏ - Put displays to sleep
Command ⌘+Control ^+Eject ⏏ - Restart
Command ⌘+Control ^+Option ⌥+Eject ⏏ - Shut down
If others like the OP have a MacBook Air or a MacBook Pro with a power key in place of an eject key, substituting the power key for eject should work in Mountain Lion and later versions of OS X, but not in Lion.
Since Mavericks, another option is to just press the power button or power key. It now puts a Mac to sleep instead of showing a dialog with different options.
<?xml version="1.0"?> <root> <item> <name>test</name> <identifier>test</identifier> <autogen>__KeyToKey__ KeyCode::S, VK_CONTROL, VK_OPTION, VK_COMMAND | ModifierFlag::NONE, KeyCode::VK_CONSUMERKEY_EJECT, ModifierFlag::OPTION_L | ModifierFlag::COMMAND_L</autogen> </item> </root>
Or create a workflow like this in Alfred:
Press the power button. Assuming you haven't reconfigured its behaviour, it should pop up a dialog with options to Sleep, Restart, or Shutdown the laptop. The "Reopen windows when logging back in" checkbox has keyboard focus by default; hitting the Tab key twice will put focus on the Sleep button - hitting the Space bar will then put the MacBook Air to sleep.
(Note that hitting Return will activate the dialog's default action - namely, to shut the computer down.)
Not as quick as a single keyboard shortcut, but faster than the mouse and without closing the lid. :)
You can assign any unused key combination (like fn⌘⌥F12) using
System Preferences »
Keyboard Shortcuts »
+ button, then under
All Applications, under
Menu Title enter
Sleep, and under
Keyboard Shortcut, press fn⌘⌥F12.
Another option, if you're more Terminal-inclined:
sudo shutdown -s now
Press the power button, then S.
As described in this thread:
The power button displays the Sleep/Restart/Shutdown dialog. If you then press S the Mac will sleep.
Keyboard shortcut is Option+Cmd+Eject (the Eject key is at your keyboard upper right corner).
I was totally happy using Karabiner but:
Karabiner does not work on macOS Sierra at the moment.
I got bored of waiting so I solved by myself.
What I need? I need a command line to sleep mac, and this is pretty easy:
Who can I run the script? This is more boring, but not complicated:
Now the easy part: open System Preferences->Keyboard and type your custom shortcut!
For everyone without a mac keyboard, I advice you to install Karabiner.
Once opened, you can go under "Change Key" tab and search for sleep:
With this option checked, now you can:
<?xml version="1.0"?> <root> <item> <name>F19 to Sleep</name> <identifier>Sleep</identifier> <autogen> __KeyToKey__ KeyCode::F19, KeyCode::VK_CONSUMERKEY_EJECT, ModifierFlag::OPTION_L | ModifierFlag::COMMAND_L </autogen> </item> </root>
To use a different F key, simply change all occurrences of
F19 to something else.
F19 works best for me since it's at the top right edge of the full-size wired Apple keyboard, making it hard to hit it by mistake and easy to find in the dark.