4

I wish to send keystrokes, including shift,ctrl,cmd,opt combinations, to OSX. Basically anything that I can do on the keyboard.

How do I accomplish this?

I would also like to send mouse clicks. Is this a separate enquiry?

4
  • stackoverflow.com/questions/3030070/… <-- I have found a duplicate, only the title is not very well worded.
    – P i
    Nov 6, 2011 at 13:04
  • Easiest way would be to use Applescript. Do you need an example > Nov 6, 2011 at 15:32
  • Yes please. Could you put it as a separate answer, if it is a viable alternative to the answer already given?
    – P i
    Nov 6, 2011 at 20:18
  • possible duplicate of Simulating key press events in Mac OSX
    – user557219
    Nov 6, 2011 at 21:00

2 Answers 2

6

You can use the CGEvent APIs, such as CGEventCreateKeyboardEvent followed by CGEventPost.

4

Using applescript you can send any key you want. Here is an example of using various methods of sending keys :

tell application "System Events"
    keystroke "h"
    keystroke (ASCII character 63)
    key code 38 -- Applescript's reference to keys
    keystroke "k" using {command down, control down}
end tell

With GUI scripting you can also send mouse clicks. These tend to be a little more involved. The following is an applescript I wrote to disable an option in System Preferences related to the Trackpad and three finger double tap.

tell application "System Preferences"
    set current pane to pane "Trackpad"
    tell application "System Events"
        tell process "System Preferences"
            if window 1's tab group 1's checkbox 3's value is 1 then
                tell window 1's tab group 1's checkbox 3 to click
            end if
        end tell
    end tell
    quit
end tell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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