0

I am trying to control google earth using the arrow keys, however, I want to do this with applescript. Essentially this code should work and I'll tell you what it actually does.

tell application "System Events"
     delay 3
     key down 124 #Value of key right
     delay 3
     key up 124
end tell 

This code should wait for me to go to google earth, then it will hold the right arrow key for 3 seconds. However, it just hits 'a'. I saw some recommendations to do the following:

key down (key code 124)

This sort of works, it press the right key only once and does not hold it.

If you do this with a key such as D (also used to navigate right in google earth) it works perfectly. (I think "key down 2" does that).

So my question is, is there a way to actually make the arrow keys work? "Working" here refers to sending a key down event that does not tie down the script and could later be canceled with a key up event. I would really like to be able to control the flight simulator (WASD does not work -- arrow keys do).

Thanks for all suggestions,

Jake

Jake
  • 348
  • 3
  • 9
  • I answered your question. If it was helpful, please "accept" the answer and vote for it. – Mark Jul 15 '13 at 12:43

1 Answers1

2

This script presses the right arrow key for 3 seconds:

tell application "System Events"
    set now to the seconds of the (current date)
    set later to now + 3
    if later > 60 then set later to later - 60
    repeat while the seconds of the (current date) is not later
        key down (key code 124)
    end repeat
end tell

I don't know if this will work with Google Earth, but I do know that the script is correct.

Mark
  • 2,380
  • 11
  • 29
  • 49
  • Well, sorry I didn't get to respond to this sooner, I am very busy. I have tried something similar to this (should have mentioned that) it seems very jittery in something like google earth and it ties up the processing for however long I want to press the key. On windows and linux, this type of operation works flawlessly. I guess the question is better stated: "Can a true key down event be used in applescript for arrow keys" (I believe the other keys do in fact work). I will update my question a little bit. – Jake Jul 21 '13 at 19:56
  • I think what I did is as good as it gets. Perhaps you should try a non-AppleScript solution. – Mark Jul 21 '13 at 21:08
  • I guess my dreams of controlling a plane via an Arduino joystick may never work on the Mac... Oh well. I guess I'll just do it on Windows. – Jake Jul 23 '13 at 13:45
  • Nice workaround, but is this truly the equivalent of keeping a key _held down_? – mklement0 Jan 31 '14 at 19:37
  • @mklement0 that seems a slightly philosophical question. When I respond to arrow keys in a programming language, keeping an arrow key pressed usually means that the same routine runs multiple times. I don't need to check if the arrow key is still being pressed and run my routine again if it is. Therefore, I'd say that pressing an arrow key a huge amount of times within a very short time is the same as keeping an arrow key pressed during that short time. – Mark Jan 31 '14 at 20:34
  • @Mark: That's not the only scenario: there are cases where you need to keep a key or key combination *held for a certain period of time* before they take effect; in other words: the keeping held effects a _single_ action, _at the end_; for instance, see http://stackoverflow.com/q/21472368/45375. Also, in your workaround you should probably use `key code` rather than `key down`, especially since there's a bug in `key down` on OS X 10.9. – mklement0 Jan 31 '14 at 21:21
  • @mklement0 Sure, you are right, but "keep a key or key combination held..." is something completely different from "keeping an arrow key pressed". It seems you are going off-topic and I suggest you ask your question elsewhere. I answered this question just a few weeks after Mavericks' official release. My answer doesn't apply to Mavericks. I'll consider a change if OP reports back. – Mark Jan 31 '14 at 22:19