Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

ANSI escape sequence for "Command" key?

I know there are escape sequences for ctrl ( ^), alt ( ~), shift ( $), etc that can be used from the command-line, but I'm wondering if there's an equivalent way to invoke the command key (or the apple key or whatever you wish to call it) from a terminal?

I'd appreciate any advice, thanks!

N/A, Mac OS X (10.4.9)

Posted on Apr 27, 2007 9:38 AM

Reply
Question marked as Best reply

Posted on Apr 27, 2007 2:55 PM

I can't make sense of your question. ANSI escape sequences begin with the Esc character and end with an ANSI-specified terminating character. They do not recognize the Mac's Command key or the Alt key and they have nothing to do with the ^, ~, or $ characters, except possibly as sequence modifiers, but these are unrelated to "ctrl", "alt", and "shift".

Now, it's possible that a smart terminal or terminal emulator, e.g., Terminal, can be programmed to do what you want, but this would be specific to your terminal, which you did not specify.
5 replies
Question marked as Best reply

Apr 27, 2007 2:55 PM in response to EvanK

I can't make sense of your question. ANSI escape sequences begin with the Esc character and end with an ANSI-specified terminating character. They do not recognize the Mac's Command key or the Alt key and they have nothing to do with the ^, ~, or $ characters, except possibly as sequence modifiers, but these are unrelated to "ctrl", "alt", and "shift".

Now, it's possible that a smart terminal or terminal emulator, e.g., Terminal, can be programmed to do what you want, but this would be specific to your terminal, which you did not specify.

Apr 27, 2007 3:06 PM in response to Ken Nellis

Ok, my fault, I was using the wrong terminology. what i mean is, if I have a bash script that I want it to emulate a key sequence like Control+C, i could use the literal string "^C".

In such cases, a carat represents Control, a tilde is Alt, and a sigil is Shift.

So my question is...is there an equivalent way to emulate, for example, Command+K?

Apr 27, 2007 5:04 PM in response to EvanK

Hi Evan,
This is more than a failure of terminology. The use of certain key sequences cause the terminal to produce certain characters that are just like letters except that many processes take special action when these characters are encountered. These characters are called "control characters."

For instance, type <Control>-v to "tell the shell" to not take special action on the next character produced. Then, if you type <Control>-c, the terminal will produce an End-of-Text character, ANSII character 3, and the shell will accept it as a literal character. However, it has no symbol for that character so it displays a pair of characters, "^C", in its place. Internally though, there is no carat and no 'C'; there is only the number three, which is the ANSII code for the End-of-Text character.

The bash shell will substitute control characters for certain escape sequences in expanding words of the form $'string'. For instance, consider the following command:

echo $'\003' | cat -v
^C

Above, the first line is a command that you can cut-and-paste into your terminal. The second line is the output you would see if you execute the command. Bash replaces the $'\003' word with the literal control character and then "cat" converts that character to the pair of characters, "^C", to show you what had been there.

Of course there are still terminology problems; you are not using the word "emulate" correctly. However, if there was such a meaning, I think that one would say that the character sequences "emulate" the control character, not the other way around.

On the other hand, when you modify a key with the "Command" key, Macintosh programs treat that as an attempt to invoke a function of that program. No character is produced. In fact technically, the program never even sees the keystrokes. The system intercepts the key event and converts it to an AppleEvent.

One tool on Macs specializes in producing AppleEvents and that is AppleScript. Fortunately, Apple wrote a utility to grant the shell access to AppleScript and that is the "osascript" command. That allows you to sort of imbed AppleScript in a shell script and that would allow you to send any program any event that the system would generate in response to a command key sequence.
--
Gary
~~~~
"Home life as we understand it is no more natural to us
than a cage is to a cockatoo."
-- George Bernard Shaw

ANSI escape sequence for "Command" key?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.