CGEventPost-模拟键盘事件时可能出现的错误? [英] CGEventPost - possible bug when simulating keyboard events?

查看:301
本文介绍了CGEventPost-模拟键盘事件时可能出现的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的代码段,旨在模拟键盘事件.下面的简单示例应键入"Cz"-Shift键按下,c键按下,c向上,Shift向上.然后z键上下移动.

I have a very simple chunk of code that is designed to simulate keyboard events. The simple example below should type "Cz" - the shift key goes down, the c key goes down, c comes up and shift comes up. Then the z key goes down and up.

尽管有时订单有时会变得混乱.当我创建一个计时器以每秒调用此例程时,输出应为CzCzCzCz....但是,这就是我得到的:

It seems that sometimes the order gets muddled though. When I create a timer to call this routine every second, the output should be CzCzCzCz.... But here's what I get:

CZcZCZCzczCzczCzczCZCZCzCz

CZcZCZCzczCzczCzczCZCZCzCz

我将再次运行它:

CzCzCzCzCZCzCZCzCZCzCZCzCZCzCzCz

CzCzCzCzCZCzCZCzCZCzCZCzCZCzCzCz

不同.同样是错误的.

代码:

e1 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)56, true);
CGEventPost(kCGSessionEventTap, e1);
CFRelease(e1);
e2 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)8, true);
CGEventPost(kCGSessionEventTap, e2);
CFRelease(e2);
e3 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)8, false);
CGEventPost(kCGSessionEventTap, e3);
CFRelease(e3);
e4 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)56, false);
CGEventPost(kCGSessionEventTap, e4);
CFRelease(e4);

e7 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)6, true);
CGEventPost(kCGSessionEventTap, e7);
CFRelease(e7);
e8 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)6, false);
CGEventPost(kCGSessionEventTap, e8);
CFRelease(e8);

在实现Shift键的keydown和keyup时我缺少什么吗?我认为这可能是一个错误-我应该在哪里报告?

Is there something I'm missing in how to implement the keydown and keyup for the shift key? I think this might be a bug - where would I report it?

推荐答案

我找到了发布修改后的键盘事件的可靠方法-它不遵循Apple文档中的示例(该方法不起作用),但似乎很有意义,最重要的是WORKS.

I have found a reliable way to post modified keyboard events - it does not follow the example in Apple's documentation (which doesn't work) but seems to make sense, and most importantly, WORKS.

您需要在按键上设置一个修饰符标志,而不是发送下移键"和上移键"消息(按照文档中的说明).这是输出大写Z的方法.

Rather than sending 'shift key down' and 'shift key up' messages (as instructed in the docs), you need to set a modifier flag on the keypress. Here's how to output an uppercase Z.

CGEventRef event1, event2;
event1 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)6, true);//'z' keydown event
CGEventSetFlags(event1, kCGEventFlagMaskShift);//set shift key down for above event
CGEventPost(kCGSessionEventTap, event1);//post event

然后我为了完整起见松开'z'键(也将shift标志设置为打开,尽管不确定这是否正确).

I'm then releasing the 'z' key for completeness (also setting the shift-flag on, though not sure if this is correct).

event2 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)6, false);
CGEventSetFlags(event2, kCGEventFlagMaskShift);
CGEventPost(kCGSessionEventTap, event2);

最后(非常奇怪),您确实需要发送Shift键的'key up'事件:

Finally (and bizarrely) you DO need to send the 'key up' event for the shift key:

  e5 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)56, false);
CGEventPost(kCGSessionEventTap, e5);

完成事件后,别忘了发布事件.

Don't forget to release your events once you're done with them.

我希望这对某人有用-我花了很长时间才使它起作用.

I hope this is useful to someone - it took me a long time to get this to work.

这篇关于CGEventPost-模拟键盘事件时可能出现的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆