Inject keyboard event into NSRunningApplication immediately after foregrounding it
问题 I am trying to bring to foreground a NSRunningApplication* instance, and inject a keyboard event. NSRunningApplication* app = ...; [app activateWithOptions: 0]; inject_keystrokes(); ... fails to inject keyboard events, but: NSRunningApplication* app = ...; [app activateWithOptions: 0]; dispatch_time_t _100ms = dispatch_time( DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC) ); dispatch_after( _100ms, dispatch_get_main_queue(), ^{ inject_keystrokes(); } ); ... succeeds. I imagine it takes a