以编程方式打开Mac帮助菜单 [英] Programmatically open Mac Help menu

查看:178
本文介绍了以编程方式打开Mac帮助菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个GTK#应用程序集成到Mac OS X中。Mac OS X上的GTK是一些Cocoa和Carbon基础的封装。我们有一些特定于平台的东西直接使用碳全局菜单API(它比Cocoa更低层次和更灵活,我们不需要是64位)。



<看起来GTK在Carbon将它们作为命令分发之前吞噬了所有的键盘事件。这是有道理的,因为没有Carbon命令到GTK世界的映射。一般来说,这不是一个问题,因为我们有一个全局的关键事件处理程序,并通过我们自己的命令系统发送所有内容。但是,这似乎阻止了Cmd-?打开帮助搜索菜单,我找不到以编程方式执行此操作的方法。



菜单管理器的MenuSelect功能很有前途,但我还没有想出一种方法自动确定坐标,出于某种原因,它只在我敲击组合两次时才起作用。

另外,一种派遣Cmd-?击中Carbon的命令直接处理或合成命令事件会很好,但我在该领域没有任何运气。



Carbon的ProcessHICommand没有任何用处一个命令ID,我不知道它是什么(如果有的话)



关于Cocoa,我可以获得NSWindow并调用InterpretKeyEvents,但是我没有任何运气成功综合NSEvent - 它只是哔哔声。我正在使用的事件是

$ $ p $ code> var evt = NSEvent.KeyEvent(NSEventType.KeyDown,System.Drawing.PointF.Empty,
NSEventModifierMask.CommandKeyMask | NSEventModifierMask.ShiftKeyMask,
0,win.WindowNumber,NSGraphicsContext.CurrentContext,?,?,
false,(ushort)keycode);

从GTK键盘映射确定键码是44.我确认键码是正确的, MonoMac(Cocoa)应用程序,但InterpretKeyEvents不适用于该应用程序中的事件。我找不到任何与该命令关联的选择器。

解决方案

您可以使用辅助功能API在菜单上按下按钮item。



pre $ NSString * helpMenuTitle = [[[[[[NSApplication sharedApplication] mainMenu] itemWithTag:HELP_MENU_TAG] title];
AXUIElementRef appElement = AXUIElementCreateApplication(getpid());
AXUIElementRef menuBar;
AXError错误= AXUIElementCopyAttributeValue(appElement,
kAXMenuBarAttribute,
(CFTypeRef *)& menuBar);
if(error){
return;
}

CFIndex count = -1;
error = AXUIElementGetAttributeValueCount(menuBar,kAXChildrenAttribute,& count);
if(error){
CFRelease(menuBar);
return;
}

NSArray * children = nil;
error = AXUIElementCopyAttributeValues(menuBar,kAXChildrenAttribute,0,count,(CFArrayRef *)& children);
if(error){
CFRelease(menuBar);
return;
}

for(id child in children){
AXUIElementRef element =(AXUIElementRef)child;
id标题;
AXError错误= AXUIElementCopyAttributeValue(元素,
kAXTitleAttribute,
(CFTypeRef *)& title);
if([title isEqualToString:helpMenuTitle]){
AXUIElementPerformAction(element,kAXPressAction);
CFRelease(title);
休息;
}
CFRelease(title);
}
CFRelease(menuBar);
[儿童释放];


I'm integrating a GTK# application into Mac OS X. GTK on Mac OS X is a wrapper over some Cocoa and Carbon fundamentals. We have some platform-specific stuff directly using Carbon global menu APIs (it's more low-level and flexible than Cocoa, and we don't need to be 64-bit).

It seems that GTK swallows up all the keyboard events before Carbon dispatches them as commands. This makes sense, because there is no mapping of Carbon commands into the GTK world. In general, this isn't a problem, because we have a global key event handler and dispatch everything via our own command system. However, this seems to be preventing Cmd-? from opening the Help search menu, and I cannot find a way to do this programmatically.

Menu Manager's MenuSelect function is promising, but I haven't figured out a way to determine the coordinate automatically, and for some reason it only works when I hit the combination twice...

Alternatively, a way to dispatch the Cmd-? keystroke to Carbon's command handling or synthesize the command event directly would be good, but I haven't had any luck in that area.

Carbon's ProcessHICommand isn't any use without a command ID and I can't figure out what it is (if there is one)

Regarding Cocoa, I can get hold of the NSWindow and call InterpretKeyEvents, but I haven't had any luck success synthesizing the NSEvent - it just beeps. The event I'm using is

var evt = NSEvent.KeyEvent (NSEventType.KeyDown, System.Drawing.PointF.Empty,
    NSEventModifierMask.CommandKeyMask | NSEventModifierMask.ShiftKeyMask,
    0, win.WindowNumber, NSGraphicsContext.CurrentContext, "?", "?",
    false, (ushort) keycode);

Keycode is determined from a GTK keymap to be 44. I confirmed that the keycode was correct using a plain MonoMac (Cocoa) app but InterpretKeyEvents did not work with the event in that app either. And I can't find any selector associated with the command.

解决方案

You can use accessibility APIs to fake a press on the menu item.

NSString *helpMenuTitle = [[[[NSApplication sharedApplication] mainMenu] itemWithTag:HELP_MENU_TAG] title];
AXUIElementRef appElement = AXUIElementCreateApplication(getpid());
AXUIElementRef menuBar;
AXError error = AXUIElementCopyAttributeValue(appElement,
                                              kAXMenuBarAttribute,
                                              (CFTypeRef *)&menuBar);
if (error) {
    return;
}

CFIndex count = -1;
error = AXUIElementGetAttributeValueCount(menuBar, kAXChildrenAttribute, &count);
if (error) {
    CFRelease(menuBar);
    return;
}

NSArray *children = nil;
error = AXUIElementCopyAttributeValues(menuBar, kAXChildrenAttribute, 0, count, (CFArrayRef *)&children);
if (error) {
    CFRelease(menuBar);
    return;
}

for (id child in children) {
    AXUIElementRef element = (AXUIElementRef)child;
    id title;
    AXError error = AXUIElementCopyAttributeValue(element,
                                                  kAXTitleAttribute,
                                                  (CFTypeRef *)&title);
    if ([title isEqualToString:helpMenuTitle]) {
        AXUIElementPerformAction(element, kAXPressAction);
        CFRelease(title);
        break;
    }
    CFRelease(title);
}
CFRelease(menuBar);
[children release];

这篇关于以编程方式打开Mac帮助菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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