CGKeyEvent粘贴工作与断点,但不是没有 [英] CGKeyEvent Pasting working with Breakpoints, but not without

查看:133
本文介绍了CGKeyEvent粘贴工作与断点,但不是没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在StackOverflow上搜索了很多关于这个主题。到目前为止我发现的东西是非常有用的。但现在,我来到一个非常有趣的事实。

I have searched on StackOverflow a lot about this topic. And what I have found so far was of great use. But now, I came to a very interesting fact.

在我的例子中,我通过隐藏应用程序NSApp hide:self

In my case, I hide the app via NSApp hide:self

然后在粘贴板中添加一个字符,然后尝试通过CGEvents在最前面的应用程序中输入它。我的代码如下:

I then add a character to the pasteboard, and then try to input it in the frontmost application via CGEvents. My Code for this is as follows:

-(void)applicationDidHide:(NSNotification *)notification{
    stringToInsert = @"©";

    NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
    [pasteboard clearContents];
    NSArray *copiedObjects = [NSArray arrayWithObject:stringToInsert];
    [pasteboard writeObjects:copiedObjects];

    //Use CGEvents to "press" Cmd-V
    CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
    CGEventRef pasteCommandDown = CGEventCreateKeyboardEvent(source, (CGKeyCode)9, YES);
    CGEventSetFlags(pasteCommandDown, kCGEventFlagMaskCommand);
    CGEventRef pasteCommandUp = CGEventCreateKeyboardEvent(source, (CGKeyCode)9, NO);

    CGEventPost(kCGAnnotatedSessionEventTap, pasteCommandDown);
    CGEventPost(kCGAnnotatedSessionEventTap, pasteCommandUp);

    CFRelease(pasteCommandUp);
    CFRelease(pasteCommandDown);
    CFRelease(source);

}

我的问题不想在从XCode 而不使用断点运行时插入文本。

My problem is that it doesn't want to insert text when I'm running it from within XCode without breaktpoints. It rather makes the mac's characteristic "plong" if he's uncapable os something.

如果我设置了一个断点到方法本身,然后,

But if I set a breakpoint to the method itself, and then jump by line, it curiously inserts it at instant.

对不起,因为我的英文错误和我可能坏的文本格式化,因为我是新的在stackoverflow。

Sorry for my bad English and for my perhaps bad text formatting as I'm new here on stackoverflow.

如果你能帮助我,我会非常感谢。

If you could help, I would really appreciate it!

推荐答案

我看到这之前,我的猜测是,事件被过于迅速地为系统处理(因为它真的只需要处理不超过,例如,20个关键事件一秒钟)。我猜猜,加入 usleep(100000)(暂停线程1/10秒)会有很大的帮助。

I've seen this before, and my guess is that the events are getting posted too quickly for the system to handle (since it really only has to handle no more than, say, 20 key events a second). I'd guess that putting in something like usleep(100000) (pause the thread for 1/10 of a second) would help quite significantly.

这篇关于CGKeyEvent粘贴工作与断点,但不是没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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