Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

OS: OS X 10.9.1 Mavericks

Is it possible to execute any global hotkeys from the command line?

For example, if I wanted to copy and paste, is there a command to let me type the exact hotkey and execute it?

Like this (This command is just for an example, doesn't actually exists):

hotkey "ctrl+c"
hotkey "ctrl+v"

I know there's already a command to copy and paste "pbpaste" and "pbcopy", I was just using copy and paste as an example.

This would be very useful to execute commands that could be very lengthy on the command line. If you wanted to lock the computer on the command line you'd have to do something like:

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

If there's a command to execute hotkeys all you had to do is:

hotkey "ctrl+shift+eject"
share|improve this question

2 Answers 2

up vote 1 down vote accepted
+50

AppleScript has the keystroke and key code commands:

osascript -e 'tell app "System Events" to keystroke "a" using command down'

osascript -e 'tell app "System Events" to key code 126 using {control down, shift down}'

There is no key code for the eject key though.

share|improve this answer
    
Great answer! But is there another way to do this without using AppleScript? If there are no none, then I will accept your answer. Thank you for taking the time to write that, I appreciate it. –  Timothy Onggowasito Feb 20 at 3:19
1  
@TimothyOnggowasito, If you're expecting to control user input (keyboard commands) there's really no other way in OS X that I'm aware of; if there was any other way Lauri Ranta might very well be the only person who would know. –  l'L'l Feb 20 at 22:35
    
@LauriRanta Looks like there are no other way, I will accept your answer and award this answer with bounty :). Thank your very very much! –  Timothy Onggowasito Feb 23 at 5:30

You can combine Lauri's AppleScript with a hotkey-based system (Quicksilver, Keyboard Maestro, etc.) to create hotkeys for running complex commands. But "hotkeys" are normally part of the GUI environment, not the CLI. They don't appear in the Terminal, although their results might if you are using Command-V to paste in text.

Have you looked into shell aliases? You could create an alias called "lock" that maps to "/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend". It's not a (GUI) hotkey, but since you asked about the CLI in particular, perhaps this will help.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.