了解 CGPostKeyboardEvent 和 CGEventCreateKeyboardEvent 之间的区别
我需要向聚焦的应用程序发送击键,我的第一次搜索引导我到 CGEventCreateKeyboardEvent:
CGEventRef eventA = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)0, true);
CFRelease(eventA);
这个编译了但没有做任何事情,但在试图弄清楚它的过程中我发现了这个............
CGPostKeyboardEvent( (CGCharCode)'a', (CGKeyCode)0, true);
并且它起作用了。为什么?从我到目前为止读到的内容来看,他们应该完成同样的事情,但是 CGPostKeyboardEvent 已弃用。
(如果我忽略了我显然应该包括的细节,请原谅我——我正在超出我通常的深度,试图帮助同事完成一个课外项目,并且只是想在此过程中学习一些东西。)
I need to send a keystroke to the focused application, and my first searches led me to CGEventCreateKeyboardEvent:
CGEventRef eventA = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)0, true);
CFRelease(eventA);
This compiled but didn't do anything, but in the process of trying to figure it out I found this...
CGPostKeyboardEvent( (CGCharCode)'a', (CGKeyCode)0, true);
...and it worked. Why? From what I've read so far about this, they should be accomplishing the same thing, but CGPostKeyboardEvent is deprecated.
(Forgive me if I'm ignoring details that I should obviously include -- I'm working way out my usual depth, trying to help colleagues with an extracurricular project, and just trying to learn a few things along the way.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您创建活动后并未发布该活动。所以下面的代码将按下向左箭头然后释放它:
Your problem is that you're not posting the event once you've created it. So the following code will press the left arrow and then release it: