8

I am trying to generate keyboard keydown and keyup events programmatically. I am currently using CGPostKeyboardEvent to do this, but I am searching for a way to do this at a lower level. I have looked at DDHidLib but could not figure out a way to create a fake event. Can anyone help?

update: Thanks to weichsel's advice I am now creating a key down event with the following:

  CGEventRef e = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)52, true);
  CGEventPost(kCGSessionEventTap, e);
  CFRelease(e);

which I found here.

However, my problem still stands so I will elaborate a bit. The application (3rd party) that I would like to send keypresses to implements DDHidLib's key capturing function:

- (void) ddhidKeyboard: (DDHidKeyboard *) keyboard
             keyDown: (unsigned) usageId;

And this never gets called using the current method. My guess is that the key down simulated with CGEventCreateKeyboardEvent is at too high of a level to be captured by ddhidKeyboard:keyDown. So what I am trying to do is create the keydown event at a sufficiently low level such that it will be recognized by ddhidKeyboard:keyDown.

Lou
  • 83
  • 1
  • 4

2 Answers2

6

What's wrong with Quartz Event Services?
I think you are on the right track, but you should use CGEventCreateKeyboardEvent instead of CGPostKeyboardEvent because the latter is deprecated since Mac OS X 10.6.

Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • Thanks! I have updated the post per your suggestion, but it unfortunately did not fix my issue. I have provided some more info about the problem. – Lou Dec 21 '09 at 18:17
  • 1
    Hmmm. Does ddhidKeyboard recognize global keypresses if the app that implements it is not the front process? – Thomas Zoechling Dec 21 '09 at 22:56
3

Event taps can inject at the HID level if you run as root

http://developer.apple.com/mac/library/documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html#//apple_ref/c/tdef/CGEventTapLocation

Azeem.Butt
  • 5,855
  • 1
  • 26
  • 22