1

I'm using this code to simulate a space down/release event in Objective-C:

 CGEventRef CGEventCreateKeyboardEvent (CGEventSourceRef source, CGKeyCode virtualKey, bool keyDown);
CGEventRef simulateSpaceUp, simulateSpaceDown;

                                      simulateSpaceDown = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)49, true);
                                      simulateSpaceUp = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)49, false);

I know its incomplete, what am I missing?

UPDATE:

Using this to post:

 CGEventPost(kCGSessionEventTap, simulateSpaceDown);

Almost there, though still not working...

Sciguy77
  • 349
  • 6
  • 14
  • possible duplicate of [How to simulate a low level keypress on os x?](http://stackoverflow.com/questions/1938509/how-to-simulate-a-low-level-keypress-on-os-x) – Carl Norum Jul 25 '11 at 22:16
  • looks like a different question to me – Sciguy77 Jul 25 '11 at 22:17
  • different question, seems like the same answer. check [this out](http://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html). – Patrick Perini Jul 25 '11 at 22:20
  • that's where I got the above code, that's what was in the docs – Sciguy77 Jul 25 '11 at 22:26

1 Answers1

1

You have to post the event using CGEventPost. Just creating it doesn't do anything.

  • Used "CGEventPost(kCGSessionEventTap, simulateSpaceDown);", though that doesn't seem to do the trick. Am I not specifying the event location? – Sciguy77 Jul 25 '11 at 23:01
  • The sample code I see online doesn't specify a source either, so that doesn't seem to be necessary. Is it possible there's something else going on when you fire the event that's keeping the spacebar from doing what you expect? –  Jul 25 '11 at 23:03
  • its possible, though that's hundreds+ of lines to go through (calls are made to other functions in other files). I'd rather just simulate a space bar press to get around the issue. Can you tell what I'm doing wrong or missing? – Sciguy77 Jul 25 '11 at 23:08
  • As far as I can tell, the approach you're using does end up simulating the spacebar appropriately -- I was able to get it to work in a test app. If it's not having the effect you intend, you'll need to give more details about what's going on. –  Jul 25 '11 at 23:50