TImer for holding a button?

Discussion in 'Mac Programming' started by Yodan1, Nov 24, 2006.

  1. Yodan1 macrumors newbie

    Joined:
    Nov 24, 2006
    #1
    Please, I have been looking for this action in Applescript for about a week now on dozens of websites, with no success. All I am looking for is a simple action that holds down a button, the X key in this case, for a specified number of seconds. Thats all. I can't find this anywhere on any resource or forum post. Does anyone have a clue? Why is it so hard to find an action for "Press this key and hold it for 10 seconds"? :confused:
     
  2. Unorthodox macrumors 65816

    Unorthodox

    Joined:
    Mar 3, 2006
    Location:
    Not at the beach...
    #2
    Dose it have to be held down or can it repeat really fast for ten seconds?
     
  3. Yodan1 thread starter macrumors newbie

    Joined:
    Nov 24, 2006
  4. Unorthodox macrumors 65816

    Unorthodox

    Joined:
    Mar 3, 2006
    Location:
    Not at the beach...
    #4
    Hmmm... OK.
    Well then I don't know.
    Out of curiosity, what do you need this for?
     
  5. Yodan1 thread starter macrumors newbie

    Joined:
    Nov 24, 2006
    #5
    Well, I've been playing the game Tremulous for a while, and just started using Speakable Items for my Mac. I've been making macros, like Reload to well...reload, and so on. Just so happens that holding X makes you run. It's been crazing my mind for a while and I can't find the answer anywhere. WASD while holding X is such a hassle. I wanted to have the script hold down X for 10 seconds when I say Run. That way, no need to hold down the awkward keys.
     
  6. Unorthodox macrumors 65816

    Unorthodox

    Joined:
    Mar 3, 2006
    Location:
    Not at the beach...
    #6
    Try this:
    Code:
    tell application "System Events"
    	tell process "Tremulous"
    		repeat 700 times
    			keystroke "x"
    		end repeat
    	end tell
    end tell
     
  7. mduser63 macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #7
    Too lazy to break out my AppleScript book right now, but I believe there are commands like key down and key up that allow you to "press" a key down, then release it later instead of giving a single keypress.

    EDIT: A quick test using Script Editor and the Keyboard Viewer seems to indicate that the following will work for you (replace 10 with the length of time in seconds that you need the key held down for):

    Code:
    tell application "System Events"
    	key down "v"
    	delay 10
    	key up "v"
    end tell
    Keys pressed down this way don't seem to autorepeat (ie you only get one "v" in TextEdit, not a bunch) but Keyboard Viewer does show that the key is pressed down for the full duration of the delay. If you need a repeated keystroke, the code that Unorthodox posted is probably better.
     
  8. Unorthodox macrumors 65816

    Unorthodox

    Joined:
    Mar 3, 2006
    Location:
    Not at the beach...
    #8
    I tested that in another game where you hold down a button to sprint and it works perfectly.
    What I suggested doesn't work and prevents you from moving. :eek: :p
     
  9. Yodan1 thread starter macrumors newbie

    Joined:
    Nov 24, 2006
    #9
    If I had arms over the internet, i would hug you. Thanks so much. You have no idea how busy I've been trying to think in simple English for easy commands in applescript. Down and up seem simple enough. Thank you all!:)
     
  10. mduser63 macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #10
    No problem, glad I could help. That's kind of the thing about AppleScript. It's really easy to read even if you don't have any experience with it, but the same is not really true of writing it. It seems like you can never figure out which plain English phrase to use to get it to work. If you are interested in learning more AppleScript stuff, check out AppleScript: The Missing Manual. It's a pretty good book and while it won't make you an expert, it'll get you to where you can at least feel comfortable enough to search through things on the web and in the documentation to figure how to do what you want to do.
     

Share This Page