CGEventCreateKeyboardEvent和CGEventTapLocation [英] CGEventCreateKeyboardEvent and CGEventTapLocation

查看:1699
本文介绍了CGEventCreateKeyboardEvent和CGEventTapLocation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难处理自定义键盘事件。我想要能够发送任何有/没有kCGEventFlagMasks的任何键,如命令,alt,ctrl,shift ...

I'm having a hard time dealing with custom keyboard events. I want to be able to send any key with / without kCGEventFlagMasks like command, alt, ctrl, shift...

我想发送当前最前面的应用程序,让我们假设它是TextEdit,组合键cmd + t,它应该显示字体窗口。

e.g. I want to send the current frontmost app, let's assume it's TextEdit, the key combo cmd+t, which should show the font window.

但目前只有t被添加到TextEdit的当前文档。我尝试发布事件与自定义集CGEventFlags和或者生成keyDown和keyUp事件为cmd周围的t事件。免费应用程序键代码显示我,当我使用CGEventFlags设置,cmd + t已按下,但此键组合不会传递到TextEdit,只有单个... ...

But currently only t is added to the current document of TextEdit. I tried posting the event with custom set CGEventFlags and alternatively generating keyDown and keyUp events for cmd around the t event. The free app Key Codes shows me when I use the CGEventFlags set, that cmd+t has been pressed, but this key combo isn't passed to TextEdit, only the single t...

这是我的代码:

...
flags = (flags | kCGEventFlagMaskCommand);
[self writeString:@"" withFlags:flags];
...

(void)writeString:(NSString *)valueToSet withFlags:(int)flags
{
UniChar buffer;
CGEventRef keyEventDown = CGEventCreateKeyboardEvent(NULL, 1, true);
CGEventRef keyEventUp = CGEventCreateKeyboardEvent(NULL, 1, false);
CGEventSetFlags(keyEventDown,0);                
CGEventSetFlags(keyEventUp,0);          
for (int i = 0; i < [valueToSet length]; i++) {
    [valueToSet getCharacters:&buffer range:NSMakeRange(i, 1)];
    CGEventKeyboardSetUnicodeString(keyEventDown, 1, &buffer);
    CGEventSetFlags(keyEventDown,flags);
    CGEventPost(kCGSessionEventTap, keyEventDown);
    CGEventKeyboardSetUnicodeString(keyEventUp, 1, &buffer);
    CGEventSetFlags(keyEventUp,flags);
    CGEventPost(kCGSessionEventTap, keyEventUp);

}
CFRelease(keyEventUp);
CFRelease(keyEventDown);

}

很难理解CGEventTapLocation的含义。我不知道哪个事件敲击将是最适合我的任务。 Quartz事件引用中的描述不会启发我:

I'm also having a hard time understanding the meaning of a CGEventTapLocation. I don't know which event tap would be the most suitable for my task. The description in the Quartz Event Reference doesn't enlighten me:

kCGHIDEventTap
Specifies that an event tap is placed at the point where HID system events enter the window server.


kCGSessionEventTap
Specifies that an event tap is placed at the point where HID system and remote control events enter a login session.


kCGAnnotatedSessionEventTap
Specifies that an event tap is placed at the point where session events have been annotated to flow to an application.

我看过很多关于这个主题的帖子,但没有一个能帮我解决我的问题。 。
感谢您的帮助!

I've seen many posts about that topic, but none of 'em helped me solve my problem... Thanks for your help!

推荐答案

这不是问题的答案,但我目前的解决方法通过AppleScript:

That's not the answer to the question but my current workaround via AppleScript:

appleScriptString = [NSString stringWithFormat:@"tell application \"System Events\" to key code %d using %@",keyCode, modifierDownString];

这是我馈送到该功能:

- (void)runScript:(NSString*)scriptText
{
    NSDictionary *error = nil;
    NSAppleEventDescriptor *appleEventDescriptor;
    NSAppleScript *appleScript;

    appleScript = [[NSAppleScript alloc] initWithSource:scriptText];
    appleEventDescriptor = [appleScript executeAndReturnError:&error];

    [appleScript release];
}

这篇关于CGEventCreateKeyboardEvent和CGEventTapLocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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