Apple - How can I map the "Eject" key on a Microsoft keyboard or create a new shortcut for locking my screen?
I don't know how to map a key to the Eject key, but you can easily create a new shortcut with Automator and Services:
Open Automator in the Applications folder.
Choose Service:
Select Utilities and drag Run AppleScript to the right pane:
Configure, on the top right corner, that the service should receive no input in any application. Then paste this AppleScript script:
on run {input, parameters} do shell script "open /System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app" end run
Save the service. Use a descriptive name, as it is the name that will appear in the Services menu. You can see in the screenshot above that I named the service Lock Screen.
Open System Preferences, select Keyboard and then the Shortcuts tab. Under General locate Lock Screen and assign a key shortcut to it. As you can see, I chose ControlCommandL:
The Lock Screen service is now accessible from any application as an entry in the Services menu:
Choose the service shortcut carefully to avoid conflicts with other applications' shortcuts.
The eject key unfortunately does not have a key code, so either they keyboard supports it or it doesn't, and you have to make a workaround. You can create a new shortcut by following these steps:
Open Automater and create a Service with a single "Run AppleScript" action, containing the following code:
tell application "Finder" to sleep
Make sure the Service is set to receive "no input", and work in "any application". Save it with a name of your choosing.
Go into Keyboard preferences, click on the Shortcuts tab, and click on Services. The newly created service should be near the bottom. Click on the service, then click on "add shortcut", and specify a shortcut to your liking.
You may have to give Automator access by adding it to the list of applications in Accessibility under the Privacy tab in Security and Privacy.
That's it! Now you can use that shortcut from anywhere to sleep your Mac.