Sending keystroke event 'paste' to a specific application Mac

163 Views Asked by At

i am using a code to simulate a 'Paste ' to the frontmost application using carbon on MacOS. Now, i need to do that to a specific application not in frontmost. I am using this code so far with the use of PostToPsn :

   ProcessSerialNumber psn;
   //Using 8844 for testing purposes (safari)
   OSStatus err = GetProcessForPID(8844, &psn);

   CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
   CGEventRef cmdDown = CGEventCreateKeyboardEvent(source,kVK_ANSI_V, YES);
   CGEventSetFlags(cmdDown, kCGEventFlagMaskCommand);
   CGEventRef cmdUp = CGEventCreateKeyboardEvent(source, kVK_ANSI_V, NO);

   if(err == noErr){

       CGEventPostToPSN(&psn, cmdDown);
       CGEventPostToPSN(&psn, cmdUp);

       CFRelease(cmdUp);
       CFRelease(cmdDown);
       CFRelease(source);

   }

It works using just a letter but when doing a combo with command, it doesn't work at all.

0

There are 0 best solutions below