Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
806 views
in Technique[技术] by (71.8m points)

c - How to simulate a low level keypress on os x?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...