2011-11-06 69 views
3

我希望向OSX發送擊鍵,包括shift,ctrl,cmd,opt組合。基本上我可以在鍵盤上做任何事情。發送擊鍵事件到OSX

我該如何做到這一點?

我也想發送鼠標點擊。這是一個單獨的調查?

+0

http://stackoverflow.com/questions/3030070/tell-system-events-to-keystroke-in-xcode < - 我已經找到一個副本,只有標題是不是很好的措辭。 –

+0

最簡單的方法是使用Applescript。你需要一個例子> –

+0

是的請。你可以把它作爲一個單獨的答案,如果它是一個可行的替代方案已經給出的答案? –

回答

6

您可以使用CGEvent API,例如CGEventCreateKeyboardEvent後跟CGEventPost

4

使用applescript你可以發送任何你想要的密鑰。以下是使用各種方法發送密鑰的示例:

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 

使用GUI腳本編制,您還可以發送鼠標點擊。這些往往會涉及更多一點。以下是我寫的一個蘋果腳本,用於禁用系統偏好設置中與軌跡板和三指雙擊相關的選項。

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