osx - Mac: Send key event to background Window -


I'm used to send key commands for applications, but it appears that it is only in front of the window In Windows for each application was sent, you can send an important event in a specific window.

I know that you can target a specific window with the help of Apple and send the main order without bringing that window to the foreground, but thinking if this program One way to do this in C / Objective-C is to have the functionality, but no documentation can be found for the API.


**** NOTE **: neither window is a window made by me, and it may be that both applications are owned by the same process.

Example : Below, I can send the command to the foreground window (up to five viewers in the above viewer), but not to do blue color without bringing the blue window to the fore Background Window (Standard Text Editor) You think that window switching is programmatic Fast, but it really deserves the attention. How is I doing this, as to copy keystrokes between windows?

Enter image details here

< Div class = "post-text" itemprop = "text">

You can do this with CGEventPostToPSN . This sample will press 'Q' KEY to send / key to TextEdit up while it is in the background.

  // action when the foreground, click a button to send the application below // 'Q' key / TextEdit key up to - (IBAction) sendQKeyEventToTextEdit: (id) This {// check if TextEdit if running ([[NSRunningApplication runningApplicationsWithBundleIdentifier: @ "com.apple.TextEdit"] count]) {// get TextEdit.app PID pid_t PID = [(NSRunningApplication *) [[NSRunningApplication runningApplicationsWithBundleIdentifier: @ " Com.apple.TextEdit "] objectAtIndex: 0] processIdentifier]; CGEventRef qKeyUp; CGEventRef qKeyDown; Processearial Number PSN; // get textEdit.app psn osetatus err = GetProcessForPID (Peed, and PSN); If (error == NoErr) {// key code view HIToolbox / Events.h to qKeyDown = CGEventCreateKeyboardEvent (zero, (CGKeyCode) 0x0C, true); QKeyUp = CGEventCreateKeyboardEvent (NULL, (CGKeyCode) 0x0C, incorrect); CGEventPostToPSN (& psn, qKeyDown); CGEventPostToPSN (& psn, qKeyUp); CFRelease (qKeyDown); CFRelease (qKeyUp); }}}  

Comments