我正在嘗試編寫一個能夠右鍵單擊OS X 10.11.5的腳本。我一直在Objective-C上使用Foundation框架來完成此任務。到目前爲止,我已經能夠成功地左鍵單擊。使用CGPostMouseEvent發佈右鍵單擊事件
以下腳本可以使用CGPostMouseEvent
左鍵單擊,其文檔可在CGRemoteOperation.h中找到。
評論提到我需要boolean_t
作爲CGPostMouseEvent中的最終參數。我不知道這意味着什麼,但我已經試過則params的下列組合無濟於事:
(PT,1,1,0,1)
(PT,1,1,(0,1 ))
(pt,1,1,2)
CGPostMouseEvent觸發右鍵單擊的正確最終參數是什麼?
#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
// grabs command line arguments -x and -y
//
int x = [args integerForKey:@"x"];
int y = [args integerForKey:@"y"];
// The data structure CGPoint represents a point in a two-dimensional
// coordinate system. Here, X and Y distance from upper left, in pixels.
//
CGPoint pt;
pt.x = x;
pt.y = y;
// This is where the magic happens. See CGRemoteOperation.h for details.
//
// CGPostMouseEvent(CGPoint mouseCursorPosition,
// boolean_t updateMouseCursorPosition,
// CGButtonCount buttonCount,
// boolean_t mouseButtonDown, ...)
//
// So, we feed coordinates to CGPostMouseEvent, put the mouse there,
// then click and release.
//
CGPostMouseEvent(pt, 1, 1, 1);
CGPostMouseEvent(pt, 1, 1, 0);
[pool release];
return 0;
}
在Mac OS 10.6中不推薦使用'CGPostMouseEvent'。 – Willeke