# How do I send event to another application?



## JFT (Jan 8, 2003)

Hi All,

I'm currently writing a driver for a usb controller. I'd like to basically send "key" events in response to the USB device buttons being hit.

I've got the USB device under control except I've got no idea how to send keyboard event to another app? My guess is that I should somehow communicate with the window server (I'm on OS 10.2.3).

The application to which I want those keyboard event sent is whichever is in focus/topmost. I'd like this to work both with  Carbon and Cocoa.

Anybody's got an idea?

Thanks
JFT

BTW I'm writing the driver for Microsoft Strategic Commander for those interested.


----------



## Ghoser777 (Jan 9, 2003)

NSDistributedNotificationCenter (There are other ways, but I think this is the easiest to set up).

Essentially you add your reciever application the DNC and have it listen for postings with a certain name and then have your sender application post a notification with that same name.

Hope this helps,
Matt Fahrenbacher


----------



## JFT (Jan 9, 2003)

Hi Matt,

Thanks for the answer. I read the NSDistributedNotificationCenter but I'm confused as to whether it's what I'm after or not?! (I'm new to Mac programming (not only Cocoa/Carbon) so bear with me if I got it all wrong  )

What confuses me is that I thought the NSNotification where application specific? Aren't NSNotification different than NSEvent?

What I'm after is a way to send "keyboard event" (generated by my driver) to both Cocoa and Carbon app (whichever is the currently active/in focus one) and that the app receiving it see no difference whether the source is the "Real" keyboard or not. Is it possible by distributing Cocoa based event or do I need lower level. I thought I needed an access to the WindowServer for this?!?

The applications to which I want to redistribute the event are not aware of this...

Thanks again


----------



## mpwiedemann (Jan 11, 2003)

Amazing, I was just logging on to ask this same sort of question. I'm also new to Mac programming (learning Carbon coming from a Windows 'C' background) and would like to know how to send a string (mimicking keyboard input) to a field in another application using Carbon. Also how do you manipulate another apps' child windows. Moving, hiding etc. The print dialog is really what I'm after. 

I know that Applescript is usually used in situations like this and I'm not opposed to using it but, am I wrong in assuming that you can't send keystrokes to this window (print dialog) using Applescript? There does not seem to be much Applescript support for the 'print' window?? I can't seem to find any information about sending keystrokes in Applescript. I have a couple of Applescript books and O'Reilly's 'Learining Carbon' and have not been able to find anything.

Thanks much!


----------



## JFT (Jan 12, 2003)

Hi mpwiedemann,

I had a hard time finding a way to do it. Either there's not of lot of traffic on this forum or people don't know or mind to answer!

Anyway here's the bit I found, mind you this is VERY low level. I guess there's another, simpler way to do it but so far I couldn't find it...

Basically you have to use IOHIDPostEvent() To be able to use this function you need to get of grasp of the EventDriver.

For an example of this download Darwin Package named IOKitUser from Apple website and look for hidsystem.subproj/HIDParamTest.c

Again there has to be a simpler way but...

Hope this Help!

P.S. I'm not done with my project so I can't guarantee 100% that the behavior is what we want!


----------



## Ghoser777 (Jan 12, 2003)

Oh, I thought you were talking about having two programs that you wrote talk to each other.  There really isn't a good way to have one application talk to another application which you don't control without the use of AppleScript, although I'm sure you could use Carbon Events, but that has to be really messy.

F-bacher


----------



## JFT (Jan 12, 2003)

Hi Ghoser,

I checked Carbon Events and made a simple attempt but failed. You think its possible using those? (Event to send to Cocoa apps?) If you do you have any lead?

What about AppleScript? How would that be done? Is it efficient?

My goal is to build a usermode driver for Microsoft Strategic Commander to use it, obviously with game but also as a "jog wheel" with audio application. The twist axe looks like it could be handy to jog along...

Thanks


----------



## mpwiedemann (Jan 12, 2003)

Depending on what application you are sending to, you may be able to use applescript. You will need to check the target apps' applescript dictionary to see if the part of the app you are interested in can be scripted. I have been doing research on Applescript for this purpose and I've found that as long as the target app is Applescript aware it can be done. The problem I'm having is that the 'print' window is not very 'AS' aware


----------



## Usman_Ismail (Dec 14, 2008)

The subject says it all, see the links below

http://openexpander.cvs.sourceforge.net/viewvc/openexpander/openexpander/
http://sourceforge.net/project/showfiles.php?group_id=247981

Let me know if you still face problems.

--regards
Usman Ismail

ps: if you are working on a similar application collaboration would be welcome as my app is GPL.


----------



## ElDiabloConCaca (Dec 14, 2008)

Yeesh, I'd hope he's still not facing problems after *5 years*!

Hint: look at the dates those were posted.


----------



## Usman_Ismail (Dec 14, 2008)

sorry did not look at at the post date.


----------

