Codec ░ Keyboard

289Simulating Keyboard & Mouse Events. And Key-Modifier Events

Creating and Posting a Keyboard Event:

CGEventRef sDown, sUp;
sDown = CGEventCreateKeyboardEvent (
			NULL,
			(CGKeyCode)1,
			<strong>true</strong>
);
<strong class="red">CGEventSetFlags(sDown, kCGEventFlagMaskShift);</strong>  
 
// setting flags with special function. 
<em>// Setting it via CGCreateKeyboardEvent
// would work only for the first time it's run</em>
 
CGEventPost(kCGHIDEventTap, sDown);
 
sUp = CGEventCreateKeyboardEvent (
			NULL,
			(CGKeyCode)1,
			<strong>false</strong>
);
CGEventPost(kCGHIDEventTap, sUp);
 
CFRelease(sDown);
CFRelease(sUp);

That leaves the door open for applying the same to mouse events:

CGEventRef mouseEvent;
mouseEvent = CGEventCreateMouseEvent (
			NULL,
			kCGEventMouseMoved,
			CGPointMake(100, 100),
			kCGMouseButtonLeft
);
CGEventPost(kCGHIDEventTap, mouseEvent );

Magical. Isn’t it.

Of course, in pre-10.6 days it would have looked like that:

CGPostKeyboardEvent (0,5,true);
CGPostKeyboardEvent (0,5,false);

569Layering one UIImage onto of another UIImage 560#import vs #include in Objective C – A quick reminder 457Singleton Classes and Shared Resources in Objective-C 420Getting current Time with Microseconds 275UIWebView – checking when user clicks a link