CGEventPost && CGEventCreateKeyboardEvent failed

Hi

I'm trying to make an application that sends keypresses to the system.

For that I'm using CoreGraphics framework. It works fine on MacOS Mojave but not on Monterey (with M1 architecture).

In each system, I allowed the application in Privacy(System preferences) to control the computer.

I'm using the following code

void postKeyCode(int keyCode, bool down){

    CGEventRef keyboardEvent =  CGEventCreateKeyboardEvent(NULL, keyCode, down);
    CGEventPost(kCGHIDEventTap, keyboardEvent);
    CFRelease(keyboardEvent);
}

Is there any additional requirements to allow the application ?

CGEventPost && CGEventCreateKeyboardEvent failed
 
 
Q