Skip navigation.
 
mlPosting Keyboard Events
FROM : Dave DeLong
DATE : Sat Sep 06 04:15:59 2008

Hi everyone,

I'm having a heck of a time figuring out how to post keyboard events. 
I've been working on this for a couple hours, googling everything I 
can think of, but I'm still stuck.

First off, I'm aware of the functions:
CGPostKeyboardEvent()
CGEventCreateKeyboardEvent()
CGEventPost()

I've tried every combination of those that I can think of.  For 
modifier keys, it "seems" to work, but when it comes to actual letter 
keys, it craps out on me.  For example, if I try to do:
   CGPostKeyboardEvent((CGCharCode)0, (CGKeyCode)56, true);
   CGPostKeyboardEvent((CGCharCode)0, (CGKeyCode)6, true);
   CGPostKeyboardEvent((CGCharCode)0, (CGKeyCode)6, false);
   CGPostKeyboardEvent((CGCharCode)0, (CGKeyCode)56, false);

I'm expecting a capital "A" to get typed.  All I get is a system beep 
once I try to type the A.  (Line 2)

So I tried a different way:

   CGEventRef shiftDown = CGEventCreateKeyboardEvent(NULL, 
(CGKeyCode)56, true);
   CGEventRef shiftUp = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)56, 
false);
   CGEventRef aDown = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)6, 
true);
   CGEventRef aUp = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)6, false);
   
   CGEventPost(kCGHIDEventTap, shiftDown);
   CGEventPost(kCGHIDEventTap, aDown);
   CGEventPost(kCGHIDEventTap, aUp);
   CGEventPost(kCGHIDEventTap, shiftUp);

Again the beep.

Then I found a reference to needing this beforehand: 
CFRelease(CGEventCreate(NULL));

Guess what!  I still get the beep.

SKIMMERS START READING HERE:

How on earth can I post system keyboard events (without getting a beep)?

Thanks a bunch!

Dave DeLong

ps - Mac OS X 10.5.4, Xcode 3.1, Mac Mini Core Duo

Related mailsAuthorDate
mlPosting Keyboard Events Dave DeLong Sep 6, 04:15
mlRe: Posting Keyboard Events Ken Thomases Sep 6, 04:56
mlRe: Posting Keyboard Events Dave DeLong Sep 6, 05:51
mlRe: Posting Keyboard Events Peter N Lewis Sep 6, 14:00
mlRe: Posting Keyboard Events Jean-Daniel Dupas Sep 6, 15:50
mlRe: Posting Keyboard Events Dave DeLong Sep 7, 16:34