Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Getting the system-wide frontmost app or key responder

Getting the system-wide frontmost app or key responder
Thread Tools
Gametes
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
Aug 15, 2006, 12:44 PM
 
I have been trying to duplicate the functionality of the "Keyboard viewer" palette - I want to have an app which, when the user clicks its buttons, pastes information (or possibly converts selections "services menu"-style) into the current highlighted view.
Only problem is, for the life of me I can't figure out how to actually physically get the active application (different from my own).
After about a dozen different hacks, I finally have a window which when clicked on does not activate the app, or change the key view at all. That is, TextEdit still is key and the text insertion point is flashing, even though I just clicked my button. But from within the action method of my button, this app is totally unaware of where the insertion point is.

Anybody know how to get the frontmost application, or the current view, or to insert text into another app?

methods which do not work:
[NSView focusView]
[NSApp keyWindow]

[NSWorkspace sharedWorkspace]
activeApplication
anything to do with firstResponder

It seems like we're protectively prohibited from interacting with other apps. You can get dictionaries of their data (including a PID, which I can't figure out how to use to send it a @selector or a message or anything like that), but you can't get the actual app.
you are not your signature
     
Gametes  (op)
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
Aug 15, 2006, 01:00 PM
 
apps known to have this behavior:
keyboard viewer
charview
accordance bible quotes
you are not your signature
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 15, 2006, 01:06 PM
 
There is no object for another app. If you want to do something like Keyboard Viewer, check out the CoreGraphics function CGEventCreateKeyboardEvent(). If you want more control over another app, you'll have to either use the accessibility API or hack it.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Gametes  (op)
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
Aug 15, 2006, 01:41 PM
 
Thanks. I have just inserted the apple sample code into my app like so:

CGEventRef event1, event2, event3, event4;
event1 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, true);
event2 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, true);
event3 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, false);
event4 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, false);

But my app just crashes when I click the button. I did explicitly import the appsupport framework stated in the header as a requirement.

But this may be a moot method, as it requires 10.4 frameworks and my target platform will be 10.3.9
How do you think all these other apps went about it?

At this point I would accept a hack, something underneath Cocoa, but I can't even find that out. I can't believe there's no method for getting the current text-insertion point.
you are not your signature
     
Gametes  (op)
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
Aug 15, 2006, 03:04 PM
 
This works, although the docs state that it's obsolete.

CGPostKeyboardEvent( NULL, (CGKeyCode)6, true);
CGPostKeyboardEvent( NULL, (CGKeyCode)6, false);

It's ironic that the new technique causes a crash. It is kind of a hack though. I'll take it...
you are not your signature
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 03:38 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,