2016-07-29 91 views
0

我正在嘗試編寫一個能夠右鍵單擊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; 
} 
+0

在Mac OS 10.6中不推薦使用'CGPostMouseEvent'。 – Willeke

回答

1

CGPostMouseEvent使用可變參數來傳遞比鼠標左鍵其他按鈕的狀態。 mouseButtonDown參數僅指示鼠標左鍵是否關閉。其他按鈕的狀態應該在功能簽名的可變參數部分mouseButtonDown之後傳遞。對於buttonCount參數,您需要傳遞所傳遞的按鈕狀態總數,包括左側按鈕。

下面的順序應該發佈一個鼠標向下的事件,然後鼠標右鍵的鼠標向上事件。

CGPostMouseEvent(pt, 1, 2, 0, 1); 
CGPostMouseEvent(pt, 1, 2, 0, 0); 

也就是說,CGPostMouseEvent已被棄用一段時間。它的替代品CGEventCreateMouseEventCGEventPost相結合,更易於使用。

+0

非常感謝你! – hotPocket