Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending keystroke events to OSX

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?

like image 807
P i Avatar asked Dec 27 '22 11:12

P i


2 Answers

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

like image 183
justin Avatar answered Jan 04 '23 23:01

justin


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
like image 33
Kassym Dorsel Avatar answered Jan 05 '23 00:01

Kassym Dorsel