How does the AXUIElementPostKeyboardEvent method use?

I want to execute the hot key operation to APP.For example the photo booth app may use the command+option+ EnterKey to complete take a picture. My code is as follows:



AXUIElementRef app=[self processIdentifierOfAppName:@"Photo Booth"];

//Method one:

AXUIElementPostKeyboardEvent(app,0,cmdKey,YES);

AXUIElementPostKeyboardEvent(app,0,optionKey,YES);

AXUIElementPostKeyboardEvent(app,0,kEnterCharCode,YES);

AXUIElementPostKeyboardEvent(app,0,cmdKey,NO);

AXUIElementPostKeyboardEvent(app,0,optionKey,NO);

AXUIElementPostKeyboardEvent(app,0,kEnterCharCode,NO);

//Method two:


AXUIElementPostKeyboardEvent(app,0,cmdKey&optionKey&kEnterCharCode,YES);

But is unable to complete this function. How can achieve this effect?

Replies

AXUIElementPostKeyboardEvent
returns an error code; what error do you get back in this case?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

The error code is 0

Because the AXUIElementPostKeyboardEvent limit used before 10.9, so the use of other methods to complete, as follows:

CGEventRef enterKey = CGEventCreateKeyboardEvent(NULL,kVK_Return, true);

CGEventSetFlags(enterKey, kCGEventFlagMaskAlternate);

CGEventPostToPid(918, enterKey);

good night!!!