identify key(s) that are pressed

Is there any way of using Terminal to identify a key or keys that is (are) pressed?

There is such a Scripting Addition (Jon's Commands: keys pressed) that works with PPC machines, but I have yet to find something equivalent for Intel machines. Rather than getting all bogged down in trying to write a new scripting addition, it occurs to me that I might be able to cheat and use a "do shell script" routine -- but that would be possible, of course, only if there's a Terminal approach that would supply the key(s) pressed information

MAC pro, Mac OS X (10.4.11)

Posted on Apr 13, 2009 1:00 PM

Reply
13 replies

Apr 14, 2009 7:09 AM in response to Noaks

So what you're asking for is a command such that if you open a Terminal window and enter a certain command with the option key held down, you get 'option' as output, but if you enter the same thing without the option key down, you get some other output. I don't know of any way to do that using only the POSIX interface. Without understanding why you would want this, I can't really suggest an alternative. You could try asking in the Applescript forum.

Apr 14, 2009 7:22 AM in response to Noaks

The terminal can not tell you if you pressed a meta key (command, option, control, shift, caps lock). This is because the Terminal is providing an ANSI Terminal interface with assumes a specify keyboard behavior.

You can see what the Terminal is passing to the programing reading from the terminal using:

cat -vte

now hit any keys you want. You will see ^ before control keys, M- before any key which has the most significant bit set (0x80 - 0xff). See "man cat".

And thus any program that uses the Terminal for input/output, will be limited to seeing these ANSI key sequences.

This web site will give you an idea of what the ANSI escape sequences you can expect from various keys
<http://h30097.www3.hp.com/docs/base doc/DOCUMENTATION/V50_HTML/MAN/MAN5/0196___.HTM>

Apr 17, 2009 8:47 PM in response to BobHarris

I apologize for not having responded to the last several posts, but it seems there is some clarification needed:

• My need is indeed to solve an issue with an AppleScript, but, at present, there does not appear to be a direct solution in AppleScript when using an Intel MAC to run the script.

• What I need to do in the AppleScript is to identify certain keys that are being held down -- when these keys are being held down, the script does one thing whereas if they are not being held down it does another. In general, it would appear that the keys held down must be "Command" and at least one other key (otherwise the "other" key may act on a window unrelated to the script which, for whatever reason, has been focussed on). So, "Command/Q" could work, whereas "Q" by itself might simply insert a string of Q's in the wrong place.

• For scripts run on a PPC MAC there is in fact a Scripting Addition called "keys pressed," included in Jon's Commands, which works perfectly for this -- but it isn't a Universal Binary and doesn't work on Intel MACS. The author told me has no plans to offer a Universal Binary version. In response to a post on the AppleScript Forum, it was suggested that I write my own Scripting Addition for Intel MACs; but it would appear that the programming skills needed to do this are beyond me at this time.

I hope the above is clear with respect to WHY I need this and WHAT the issues are.

• Since AppleScript can interact in significant ways with Terminal, I've turned to this forum to see if there is any way --using Terminal -- to identify keys that are being held down. I may be wrong, but it would seem to me that the system must have a unique reaction to any single key being held down. One of those keys must be "Command" (as the script must continue to work properly on PPC MACs), and I think the "other" key can be just about anything.

In summary, and as an example, my question is this: Is there anything I can enter on the Terminal command line that will output code which indicates that the "Command" and "Q" keys are depressed; +those actual key names need not appear in the output, as long as there is code that I can interpret as those names+. I'll worry about getting the proper information into the AppleScript.


Hope this is clear.

Message was edited by: Noaks

Apr 18, 2009 5:00 AM in response to Noaks

• Since AppleScript can interact in significant ways with Terminal, I've turned to this forum to see if there is any way --using Terminal -- to identify keys that are being held down. I may be wrong, but it would seem to me that the system must have a unique reaction to any single key being held down. One of those keys must be "Command" (as the script must continue to work properly on PPC MACs), and I think the "other" key can be just about anything.

Yes, Mac OS X can detect any key that is held down and it can tell you if it was the left or right Command key, however, Terminal is not going to tell you that, as Terminal sends nothing when Command, Option, Shift, Control, Caps Lock, are held down. Terminal will only send ANSI key sequences.

And for the most part Unix oriented stuff tends to focus on Terminal oriented or X11 oriented applications.

You need to find some other utility, or as you are working with Applescript, something that plays nice with Applescript.

Jun 14, 2009 11:25 PM in response to BobHarris

<You need to find some other utility, or as you are working with Applescript, something that plays nice with Applescript.>

Well, that's exactly what Noaks is asking at the end of the day. I understand he wanted to use a do shell thinking the Terminal could do that.

I am quite interested by finding a solution to this problem also.
Let's say I am in a repeat loop in AppleScript and I want to exit the loop when a specific key is pressed... so a test somewhere inside the loop would be made to see if a key is being held down at the time the test is being made and which one.

Noaks, if youy have found a solution to this I would be glad if you share it.

Thanks to all

Jun 15, 2009 10:54 PM in response to RanjeetJones

RanjeetJones, you are wrong. On first post Noaks talks about Scripting Additions. There use to be one giving the possibility of just what we want to do, but on PPCs only. The purpose of his and my question is to find an equivalent solution on Intels. High-Level languages can talk to hardware as long as it is implemented, that's all. AppleScript doesn't have much implemented by itself but can be well upgraded in that regard. Of course then if one want to do this by oneself then one have to dig into lower-level languages. Just trying to find that personn I guess.

Jun 15, 2009 10:59 PM in response to BobHarris

Bob, you are very right. I ended on this thread directly after some internet research and just noticed thanks to you it is in Unix forums. Very wrong and should have been pointed on first reply to Noaks to avoid these unnecessary posts. This is my last one here.

I guess the one thing that brought Noaks here is that wanting to use the terminal with a do shell from Applescript, he thought Unix would be a good place.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

identify key(s) that are pressed

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