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?
我试图以编程方式生成键盘keydown和keyup事件。我目前正在使用CGPostKeyboardEvent来执行此操作,但我正在寻找一种在较低级别执行此操作的方法。我看过DDHidLib,但无法找到创建假事件的方法。有人可以帮忙吗?
update: Thanks to weichsel's advice I am now creating a key down event with the following:
更新:感谢weichsel的建议,我现在正在创建一个关键事件,其中包含以下内容:
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:
但是,我的问题仍然存在,所以我会详细说明一下。我希望发送按键的应用程序(第三方)实现DDHidLib的键捕获功能:
- (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.
并且永远不会使用当前方法调用它。我的猜测是,使用CGEventCreateKeyboardEvent模拟的键值太高,无法通过ddhidKeyboard捕获:keyDown。所以我要做的是在足够低的级别创建keydown事件,以便ddhidKeyboard:keyDown识别它。
2 个解决方案
#1
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.
Quartz Event Services有什么问题?我认为你走在正确的轨道上,但是你应该使用CGEventCreateKeyboardEvent而不是CGPostKeyboardEvent,因为自Mac OS X 10.6以来不推荐使用后者。
#2
3
Event taps can inject at the HID level if you run as root
如果以root身份运行,事件点击可以在HID级别注入
#1
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.
Quartz Event Services有什么问题?我认为你走在正确的轨道上,但是你应该使用CGEventCreateKeyboardEvent而不是CGPostKeyboardEvent,因为自Mac OS X 10.6以来不推荐使用后者。
#2
3
Event taps can inject at the HID level if you run as root
如果以root身份运行,事件点击可以在HID级别注入