EDIT - Don't get a lot of input into this, so it's skinny here. I am sending keyboard events to PSN. Then I will switch to another window, posting a few more events (this time tat session level) and return to the first window. When I go to PSN again, nothing happens. Until I move the mouse wheel or scroll wheel. Why is this and how can I get around this (if not corrected)?
ORIGINAL - If I set up a loop that places some keyboard events on the PSN, I find that it works fine, except when it was first run. The event seems to be published when I do something with the mouse manually - even moving it slightly. Here are the details if they help.
The external application has a window for list of text strings, which I read by sending copy commands (and checking cardboard). Unfortunately, this is the only way to get this text.
Sometimes an application distracts focus from a list that I can detect. When this happens, the most reliable way to get the focus back is to send a mouse event to click on the text box directly above the list, and then send the tab event to shift the focus to the list.
So, when you start the loop works fine, scrolling through the list and copying the text. When the focus is shifted, its detection is beautiful, and events are sent to move the focus back to the list. But nothing happens. The loop continues to detect that the focus has changed, but events only work after moving the mouse. Or just use the scroll wheel. It’s strange.
As soon as this happened for the first time, it works fine - every time the focus moves, PSN events change it, without me doing nothing.
Here the code that works in the loop is checked as working:
//copy to pasteboard - CMD-V e3 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)8, true); CGEventSetFlags(e3, kCGEventFlagMaskCommand); CGEventPostToPSN(&psn, e3); CFRelease(e3); e4 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)8, false); CGEventPostToPSN(&psn, e4); CFRelease(e4); //move cursor down e1 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)125, true); CGEventPostToPSN(&psn, e1); CFRelease(e1); e2 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)125, false); CGEventPostToPSN(&psn, e2); CFRelease(e2);
And here, where I switch focus, I also work (except when necessary):
//click in text input box - point is derived earlier e6 = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, point, 0); CGEventPostToPSN(&psn, e6); CFRelease(e6); e7 = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, point, 0); CGEventPostToPSN(&psn, e7); CFRelease(e7); //press tab key to move to chat log table CGEventRef e = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)48, true); //CGEventPost(kCGSessionEventTap, e); CGEventPostToPSN(&psn, e); CFRelease(e); CGEventRef e11 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)48, false); CGEventPostToPSN(&psn, e11); CFRelease(e11);