54

Title says it all: how do I lock the screen using a keyboard shortcut on OS X Mountain Lion with a Microsoft Natural Ergonomic Keyboard 4000?

bassplayer7
  • 14,106
mattvonb
  • 640

17 Answers17

40

You do not need to use Third Party Apps to set a global short cut. You can use Automator Services,System Preferences keyboard Shortcuts and the CGSession -suspend command that switches to a login screen.

Use the Applescript :

do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"

in a Automator 'Service' like this with the set up as no input and all applications.

enter image description here

Save it and then go to the Keyboard System Preferences. -> Keyboard Shortcuts tab. Select Services on the right. And scroll down to the bottom on the left to 'General'. There you will see your service. Click the addShortCut. And give the service a shortcut. Clost system prefs.

enter image description here

You may need to quit some apps and re open them first for them to pick up the short cut for the first time. You can see the ones that have already picked it up..

enter image description here

Now try the shortcut from the keyboard.

markhunte
  • 12,242
  • I get really weird behavior in Mavericks with this method. – diimdeep Oct 28 '13 at 15:56
  • weird behaviour like what?? – markhunte Oct 28 '13 at 17:20
  • UI is freezing and completely broken like that https://dl.dropboxusercontent.com/u/609809/apple.stackexchange.com/a.79128.7049/login_window_broken.jpg another time UI was broken too but different. – diimdeep Oct 28 '13 at 20:16
  • 4
    +1, this introduces some really useful general knowledge too! thanks! – gatoatigrado Mar 20 '14 at 21:33
  • Can I maybe get it to activate the screen saver right away too? – metakermit May 14 '14 at 07:07
  • 1
    @kermit666 yes. use: do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine" – markhunte May 14 '14 at 17:12
  • This answer is not working with 10.9.5 - is there something that needs to be added to Accessibility / Privacy / Assistive Access? – cwd Sep 30 '14 at 05:14
  • It does on mine. Just tested it? – markhunte Sep 30 '14 at 06:21
  • Still works on 10.10.3. You can simplify the Automator script a little, choose "Run Shell Script" (From Utilities section on the left panel of actions), and then paste: /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend – Ethan Apr 30 '15 at 01:18
  • 5
    If you want to immediately sleep your display, you can also just do a similar trick with a different shell command: pmset displaysleepnow. – Glyph May 13 '15 at 10:16
  • @Glyph how do you map your command to an external keyboard? I have no eject button or power off button. – Pavan Mar 14 '17 at 00:31
  • 1
    @Pavan - this answer explains exactly that. If you follow the instructions you will see they don't reference "power off" or "eject", just control, command, and right bracket. – Glyph Mar 15 '17 at 06:10
  • I prefer pmset displaysleepnow because upon wake, it shows clickable user icons, rather than a username field that needs to be manually filled in – Alexander Mar 27 '18 at 17:13
  • Nitpick on an otherwise great answer - you appear to have left and right reversed in the section beginning "Save it and then go to the Keyboard System Preferences". Otherwise, top-notch, thank you! – scubbo Mar 27 '18 at 19:25
  • I take it back - although my Macbook (Pro, 13-inch, 2016, MacOS 10.12.6) could wake up easily from this state after being left in it for a few seconds, when left for half an hour it locked up and required a restart. – scubbo Mar 27 '18 at 20:13
32

Or you can go to: Applications > Utilities > Keychain Access > Preferences > Show keychain status in menu bar.

This will put a lock in your menu bar and you can click Lock Screen to lock your screen. If you need a keyboard shortcut, you can add this in your keyboard under Settings.

Keychain Access menu

Also, here are steps to use a shortcut to put your computer in screen saver mode which locks your Mac at the same time.

  1. Open System Preferences.
  2. Select Security & Privacy.
  3. Select General.
  4. Be sure Require password [immediately] after sleep or screen saver begins is selected.
  5. Go to your applications folder.
  6. Open Automator.
  7. Select Services on the screen that appears.
  8. At the top of the new Service's actions, in the Service receives drop-down, select no input from the options. Make sure that any application is selected in the second drop-down.
  9. Add the Start Screensaver action (in the Utilities group of actions) to the Service by dragging it to the right.
  10. Save the Service (Automator does not ask you where to save it, just to name it Logout for example).
  11. Next, open System Preferences again and select the Keyboard preference pane. Select the Shortcuts tab at the top, then the Services group on the left.
  12. The service you created should be near the bottom of the list of Services under the General disclosure triangle.
  13. Double-click on the right side of the entry for the Service you created and assign a keyboard shortcut.
  14. I chose Command-Shift-L for my shortcut.
  15. Exit the keyboard preference pane and give it a try.

You can always use Control+Shift+Eject (if you have a mac keyboard) or Control+Shift+Power on your mac to put your computer to sleep, but I don’t want that when I can just close the mba lid. We all know about hot corners, but I’m not a big fan of it as it always interferes with other options.

I had been using the lock menu icon for the longest time found in Applications > Utilities > Keychain Access > Preferences > Show keychain status in menu bar but it just takes too long sometimes to click that small icon when I’m in a rush since the target is so small.

Evan
  • 421
21

UPDATE - 2021 answer is - ctrl-win-q

legacy below I used KeyRemap4Macbook to remap the pause/break key to eject. KeyRemap4Macbook is free, and it shows up in System Preferences after installing.

shift+ctrl+pause/break does the trick now.

screenshot from keyRemap4macbook

For newer versions of the OS (Sierras, El Cap) there is the new Karabiner-Elements with a slightly different interface:

screenshot from Karabiner-Elements

**Please note that KeyRemap4Macbook is now called Karabiner with the same functionality.

**update for Sierra - Karabiner ** new karabiner edit

BentheFolker
  • 329
  • 2
  • 6
  • I downloaded this and it came with a lot of malware. – mac10688 Aug 18 '15 at 14:47
  • 1
    Can you list specific malware? I have been using karabiner for three years, installing all updates, and have never had anything extra come along. Here is the official: https://pqrs.org/osx/karabiner/ – BentheFolker Aug 19 '15 at 16:03
  • The creator, Takayama Fumihiko, has this to say:

    "Unfortunately, I don't have enough reputation to comments.

    If you can, please tell to mac10688 that he/she must download binary from official site. https://pqrs.org/ And Karabiner is an open source project and designed with security in mind. https://github.com/tekezo/Karabiner "

    – BentheFolker Aug 19 '15 at 17:13
7

Open System Preferences -> Keyboard -> Shortcuts -> App Shortcuts. Click on +. Select All Application for Application, enter Sleep for Menu Title, press the desired shortcut in the Keyboard Shortcut field. Click on Add. See the Apple menu (an apple icon in the top left corner). The Sleep entry should show the shortcut you have defined. The shortcut should work right away.

Screenshot

Note that Finder and possibly some other applications would only react to the shortcut after the reboot.

proski
  • 286
7

Assuming your keyboard has an eject button, you need to go to System Preferences, and click on "Security & Privacy", then click on the "General" tab, then select the checkbox that says "Require password immediately after sleep or screensaver begins". Now you can lock your computer by pressing Control-Shift-Eject. This puts the computer to sleep, but because we've told it to lock on sleep, it also locks it.

If your computer doesn't have an eject button, you will need to create your own custom shortcut. Unfortunately, you can only create shortcuts for menu items and services. Fortunately you can create a service to put the display to sleep (and thus lock the screen), and you can assign a shortcut to the service. For instructions on how to do this, see this Macworld page.

daviewales
  • 1,507
  • 1
    There is no eject button on the keyboard. (Or if there is, I cannot find it.) Also, I do not want the computer to sleep, only to be locked. – mattvonb Dec 05 '12 at 22:43
  • You can replace the Eject button with the Power button. (If the keyboard has a power button...) – daviewales Dec 05 '12 at 22:46
  • 1
    The keyboard also does not have a power button. – mattvonb Dec 05 '12 at 22:48
  • There is an article here which shows you how to create your own custom shortcut. http://hints.macworld.com/article.php?story=20090831093941225 – daviewales Dec 05 '12 at 23:09
4

You can easily add the padlock icon to your menu bar:

open /Applications/Utilities/Keychain\ Access.app/Contents/Resources/Keychain.menu

You can also add it through Keychain Access prefs. ;)

nohillside
  • 100,768
2

By default, you can use Ctrl+Command+Q.

  • Welcome to Ask Different! We're trying to find the best answers and those answers will provide supporting info as to why they're the best. Answers should be self-contained so explain why you think the answer you provided will solve the problem or is better than others out there. Providing links as supporting information can also help the OP, and others, find additional info for themselves. See [answer] on how to provide a quality answer. - From Review – fsb Sep 14 '18 at 20:56
  • upvoting here as this is standard in the latest MacOSs - Catalina, at the time of this comment. Thank goodness Apple added an easy shortcut. – BentheFolker Nov 15 '19 at 14:13
2

Control+Shift+Eject is the keystroke for Macs with an Eject key, and for external keyboards Control+Shift+Power is the keystroke for Macs without the eject key, like the MacBook Air and MacBook Pro Retina

http://osxdaily.com/2011/01/17/lock-screen-mac/

1

Note that there's also an alternative method which frees you of remembering obscure key combinations and uses English (or other) words instead: with Alfred or possibly Quicksilver (not sure tho), you can invoke actions such as locking the screen, all via a single hotkey and a bunch of commands in plain language. OSX's Spotlight is moving in the same direction — but I'm not sure if it's quite there yet; though you probably can run AppleScript files with it.

aaa
  • 153
1

I stand corrected, trach a man to fish:

https://support.apple.com/en-us/HT201236

  • This probably doesn't work for Mountain Lion, so this doesn't answer the question in a pedantic sense, but most people probably aren't using Mountain Lion anymore. – mattvonb Jan 08 '20 at 22:25
1

You can also log out to the fast user switching screen, but it doesn't turn off displays. Just assign a shortcut to this shell command:

/System/Library/CoreServices/Menu\ Extras/user.menu/Contents/Resources/CGSession -suspend

If you have the Alfred Powerpack, you can give the lock action a shortcut in the hotkeys tab:

Lri
  • 105,117
  • there are 100 articles pointing to a workaround. This is actually the answer I was looking for because I don't freaking want to set the "require password" to "immediately". +100 points – nils petersohn Nov 26 '15 at 09:09
  • It's a good solution, but unfortunately, it makes the iTunes halt the currently playing track, in contrast to the Keychain "Lock Screen" method. – Elad Nava Jan 02 '16 at 21:30
0

You can install the tiny, free SleepDisplay application, and assign it to any key in the Microsoft Keyboard section of System Preferences.

0

@markhunte's answer is good, but it falls short when you're in an app that doesn't have a "Services" menu (e.g., Calculator).

Instead of using System Preferences > Keyboard > Shortcuts to assign the shortcut, I use Apptivate: it's a free, lightweight utility whose only purpose is to run a command when you press a hotkey sequence.

Once you've created a "Lock Screen" service in Automator (as described by @markhunte's answer), you simply add it to Apptivate:

Empty Apptivate menu

When the "Open" dialog is displayed, press CMD+SHIFT+G (Go to Folder) and enter ~/Library/Services:

Go to Folder dialog

Then select the Automator service you created earlier:

Select Automator service

Finally, assign a keyboard shortcut to the service:

Apptivate keyboard shortcut menu

And that's it! Now you have a "Lock Screen" keyboard shortcut that can be used in any application.

0

My system version is Yosemite 10.10.5. And the method posted by markhunte is still working except that you need input username everytime after you suspend the system. It's a little annoying to me. So changed the script from:

do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"

to:

tell application "System Events" to sleep

This script just simulates that you click the sleep menu item of system menu.

0

If you use the new MacBook Pro or a Windows / external keyboard, use a program like Karabiner Elements to map from a key you choose to "Eject", so that the good old CTRL-SHIFT-EJECT works.

For example:

Karabiner Elements

dux2
  • 101
-1

There's a little app which does the trick — http://keylock.io

Bryan
  • 11
-1

Ctrl+Shift+PwrBtn

Using an original iMac orange keyboard on a 2011 MacBook Pro.