Simulating keystrokes with CGEventCreateKeyboardEvent

Hi there,

Has anyone had any luck simulating key strokes with CGEventCreateKeyboardEvent on OS X.

I am trying to make an app that simulates key strokes and a little bit lost.

I think CGEventCreateKeyboardEvent is available through ApplicationServices.framework, but I can’t figure out how to get it to do something.

I have tried hacking the events example, and thought that adding this line (below) might simulate a keystroke to the screen when I click the mouse.

It compiles ok, but does not do anything…

Any tips on how to do this would be really appreciated.

Thanks for looking

simon

  
  
//--------------------------------------------------------------  
void testApp::keyPressed  (int key){   
	sprintf(eventString, "keyPressed = (%i)", key);  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
	CGEventCreateKeyboardEvent (NULL, (CGKeyCode)52, true);  
}  
  

excuse talking to myself but I just found something that works. Now I need to work out how to send the keystroke to another app. :lol: If anyone has attempted this sort of thing on os X I would love to hear as it seems pretty arcane. Googling just seems to bring up loads of forums of people struggling with it.

Cheers

Simon

  
  
  
//change within eventsExample:  
  
//--------------------------------------------------------------  
void testApp::keyPressed  (int key){   
	sprintf(eventString, "keyPressed = (%i)", key);  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
	CGEventRef e = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)52, true);  
	CGEventPost(kCGSessionEventTap, e);  
	CFRelease(e);  
}  
  
  

I suspect your problem is that keyboard focus can only be on one application - in order to trigger the keybaord event code you are using the mouse click - so the open frameworks app will have keyboard focus, so you can’t type in another app, the keystrokes generated will be received by the openframeworks app.

If you get the keyboard event to trigger by another method so that the opeframeworks app doesnt get keyboard focus, run your app and click on the window you want to receive the keyboard events you should get results.

HTH

Hey thanks Nick,

You have a point there.

  
  
  
void testApp::mouseMoved(int x, int y ){  
	 CGEventRef e = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)52, true);  
         CGEventPost(kCGSessionEventTap, e);  
         CFRelease(e);  
  
	}  
  

something like that has the strange effect of writing in Xcode when you run it from Xcode but into the app when you run the app. However it still won’t write into another app like text edit though.

I think for that you must need to get the ProcessSerialNumber of the app you want (which I am not quite sure how to do). But then its something like this…

  
  
	ProcessSerialNumber psn;   
	GetFrontProcess(&psn); // how can you get another apps psn ?  
	CGEventRef e = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)83, true);  
	CGEventPostToPSN (&psn,e);  
	CFRelease(e);  
  

Thanks for the tip

hi all

i can use this for windows too?? or whit how can i make this for windows, tha alll :slight_smile:

In Windows I think you would have to use something called sendKeys instead.

Have a look here.
http://msdn.microsoft.com/en-us/library-…-dkeys.aspx

Let me know if you get it working… I am still struggling on a Mac trying to get my head around getting hold of different processes.

I thought this might work but it only produces a keystroke in the oF app if it is active… If text edit is in the foreground nothing happens in oF or text edit

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

Cheers

simon

I got sendkeys working on windows, but have lost the project - will be trying to rebuild it in a couple of weeks.

I’m sure i got the mac one working as well - but my memory may be tricking me - i may have just thought - this is how i will get it to work, but did not test it .

HTH

[quote author=“nick”]I got sendkeys working on windows, but have lost the project - will be trying to rebuild it in a couple of weeks.

I’m sure i got the mac one working as well - but my memory may be tricking me - i may have just thought - this is how i will get it to work, but did not test it .

HTH[/quote]

Hi nick, you can make again this project, im am tried to make this but i am lost…

thank you if you can helpme

use this to avoid finding the correct CGKeyCode

  
CGEventRef e = CGEventCreateKeyboardEvent( NULL, 0, true );  
        UniChar oneChar = 'Y';  
        CGEventKeyboardSetUnicodeString(e, 1, &oneChar);   
        CGEventPostToPSN (&safariProcessNumber,e);    
        CFRelease(e);   

and this to find an application

  
// search safari  
        ProcessSerialNumber safariProcessNumber;  
        ProcessSerialNumber psn = { 0, kNoProcess };  
          
        while (noErr == GetNextProcess(&psn)) {  
              
            CFStringRef processName;  
            if (noErr == CopyProcessName(&psn, &processName)) {  
                string w = MYCFStringCopyUTF8String(processName);  
                if(w=="Safari") safariProcessNumber = psn;  
  
            }  
        }  

greetings ascorbin

anyone knows how to trig some key press in Windows from the OF app? I see the sendKeys link but I don’t know how to start…