Sending keyevents to another application

Does anyone know a way, to send key events I receive in an openframeworks-application to another application, which is running in the background at this moment (as the of app has the focus)?
I’m on OS X Leopard.


Hmm, I know the basic ideas of it, but haven’t done it for a while, so I can’t give specific code samples.

I would recommend doing some research on “multi-process communication” or “inter-process communication”. There are a few methods you will find… Pipes/Named Pipes, Shared Memory, Message Queues, Semaphores, and likely a couple of others. They all have advantages and disadvantages; but for your purpose of passing on keystrokes, I think a message queue would be most suitable.

Here is a link to a pretty nice resource that explains them all with code examples.

This one is Unix focussed, but I’m thinking that it should still work on OSX (since it’s unix based). If you find it doesn’t work, maybe try searching for “OSX multi-process communication” or something like that.

on mac you should be able to do it with applescript

this works on windows?

I would go with applescript aswell, why reinvent the wheel and all that.

Applescript is OSX only, you can probably do the same thing on windows though, if not
directly then with a 3rd party app probably.


I started a thread on this over on advanced (not that I am advanced, just thought it was an odd thing to want to do)

On OS X it seems like you can tap into QuartzEventServices.…-rence.html

I don’t really know much about AppleScript but I think it could be done fairly easily this way too. This works but at the moment I am stuck on capturing the ProcessSerialNumber of an application other than oF.

if (ofGetElapsedTimeMillis() == 4000){  
	ProcessSerialNumber psn;  
	GetFrontProcess( &psn );  
	CGEventRef e = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)42, true);  
	CGEventPostToPSN (&psn,e);  

Looking forward to hearing more thoughts about how best to do this.




If anyone is still following this thread… this works on OS X :smiley:

It does not use apple script but I would still like to see some examples of that.

void testApp::draw(){  
	sprintf (timeString, "time: %0.2i:%0.2i:%0.2i \nelapsed time %i", ofGetHours(), ofGetMinutes(), ofGetSeconds(), ofGetElapsedTimeMillis());  
	if (ofGetElapsedTimeMillis() >= 8000 && ofGetElapsedTimeMillis() <= 8100){  
	ProcessSerialNumber psn;  
	GetFrontProcess( &psn );  
	CGEventRef e = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)42, true);  
	//CGEventPost(kCGSessionEventTap, e);  
    //CGEventPost(&psn, e);  
	CGEventPostToPSN (&psn,e);  

Try adding it to a sketch an you have 8 seconds to bring another app to the foreground (It worked for me with Text edit and mail).

You should get 100 milliseconds worth of /////// appear in foreground app.

I think the problem with my previous code was

 if (ofGetElapsedTimeMillis() == 8000){}  

I think there is a timing issue where you need a little more time to send the keystroke.

Any variations on the above code would be greatly appreciated… Ascii Art… Generative e-mails anyone ?



Hi all

if anyone foloww this thread, can helpme with the way for make the same in windows??

i need to interprocess comunication…

i want to make this caind of games, for school project

I hope anyone can guide me i need i way to take, i have the optical flow part, i am loose in keystrokes simulate in other windows…

This will print the letter h in the frontmost window in Windows.



here ->