为最前面的应用程序生成键盘事件

为最前面的应用程序生成键盘事件

本文介绍了为最前面的应用程序生成键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mac OS / Cocoa中,我可以以透明的方式合成键盘输入 - 字符串 - 用于最前面的应用程序?



更精确,我不想发送特殊字符或控制序列。我只需要发送标准字符



刚学习了,AppleScript可以这样做:

 告诉应用程序TextEdit
activate

告诉应用程序系统事件
keystrokefusing {command down}
end tell
end tell

问:我该如何使用ObjC / cocoa? p>

更新2012-02-18 - Nicks提案已增加



基于的代码,这里是最终的解决方案:

  //首先,获取当前应用程序的PSN 
ProcessSerialNumber psn;
GetFrontProcess(& psn);

//做一些关键事件
CGEventRef keyup,keydown;
keydown = CGEventCreateKeyboardEvent(NULL,(CGKeyCode)6,true);
keyup = CGEventCreateKeyboardEvent(NULL,(CGKeyCode)6,false);

//将它们转发到最前面的应用程序
CGEventPostToPSN(& psn,keydown);
CGEventPostToPSN(& psn,keyup);

//最后表现为友好
CFRelease(keydown);
CFRelease(keyup);

使用此方法,点击非激活面板的按钮将事件定向到实际前应用程序。完全是我想做的。

解决方案

当然,你会想使用CGEventCreateKeyboardEvent来创建键盘事件,例如:

  CGEventRef keyup,keydown; 
keydown = CGEventCreateKeyboardEvent(NULL,(CGKeyCode)56,true);
keyup = CGEventCreateKeyboardEvent(NULL,(CGKeyCode)56,false);

CGEventPost(kCGHIDEventTap,keydown);
CGEventPost(kCGHIDEventTap,keyup);
CFRelease(keydown);
CFRelease(keyup);

这比AppleScript有点复杂,但它做的伎俩。你必须显式发布一个键下来,然后一个keyup事件。有关详情,请参阅。 p>

In Mac OS / Cocoa, may I synthesize keyboard entries - strings - for the frontmost application in a transparent way?

To be more precise, I don't want to send special characters or control sequences. My only need is to send standard characters.

Just learned here, that AppleScript can do the trick like this:

tell application "TextEdit"
    activate

    tell application "System Events"
        keystroke "f" using {command down}
    end tell
end tell

Q: How would I do this using ObjC / cocoa?

UPDATE 2012-02-18 - Nicks proposal enhanced

Based on Nick's code below, here is the final solution:

// First, get the PSN of the currently front app
ProcessSerialNumber psn;
GetFrontProcess( &psn );

// make some key events
CGEventRef keyup, keydown;
keydown = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, true);
keyup = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, false);

// forward them to the frontmost app
CGEventPostToPSN (&psn, keydown);
CGEventPostToPSN (&psn, keyup);

// and finally behave friendly
CFRelease(keydown);
CFRelease(keyup);

Using this method, a click on a button of a non-activating panel targets the event to the actual front application. Perfectly what I want to do.

解决方案

Sure, you'll want to use CGEventCreateKeyboardEvent to create keyboard events, then post them as such:

    CGEventRef keyup, keydown;
    keydown = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, true);
    keyup = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, false);

    CGEventPost(kCGHIDEventTap, keydown);
    CGEventPost(kCGHIDEventTap, keyup);
    CFRelease(keydown);
    CFRelease(keyup);

It's a bit more complicated than AppleScript but it does the trick. You do have to explicitly post a keydown and then a keyup event. More information at the Quartz Event Services Reference.

这篇关于为最前面的应用程序生成键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:44