2

我目前正在开发和 AIR 应用程序来检测组合键的命令键,目前我有这样的东西

this.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);

protected function onKeyUp(event:KeyboardEvent):void
{
  if(event.ctrlKey){
    switch(event.keyCode){
      case Keyboard.C:
      trace("need to copy");
      break;
    }
  }
}

根据文档 event.ctrlKey,应该检测 mac 上的命令和控制键,如果我按下组合控制 + C 作品,但如果我不使用命令 + c 什么,我也尝试过

event.commandKey

我以为内置菜单可能给我带来了一些麻烦,然后我将其删除,但仍然没有,我错过了什么?谢谢你的帮助

4

1 回答 1

0

我也遇到了同样的问题,并通过为剪贴板键添加本机菜单事件来解决此问题。正如您所说,“event.ctrlKey”不适用于 MAC,而“命令键”与 C(copy)、V(paste)、X(cut)、W(close)、M(minimize) 结合使用将始终收听AIR 应用程序的本机菜单事件(buildIn 菜单事件)。所以我添加了菜单选择事件。

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       creationComplete="windowedapplication1_creationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
            {
                this.addEventListener(KeyboardEvent.KEY_DOWN, onKey_PressHandler);
            }

            protected function onKey_PressHandler(event:KeyboardEvent):void
            {
                // for mac only statements
                if(isMAC())
                {
                    if(NativeApplication.supportsMenu){
                        if(event.keyCode == Keyboard.COMMAND)
                        {
                            if(event.type == KeyboardEvent.KEY_DOWN)
                            {
                                NativeApplication.nativeApplication.menu.addEventListener(Event.SELECT,onApplicationMenu_ClickHandler);
                            }
                            else
                            {
                                // remove event if it is no longer needed 
                                NativeApplication.nativeApplication.menu.removeEventListener(Event.SELECT,onApplicationMenu_ClickHandler);
                            }
                        }
                    }
                }
            }

            protected function onApplicationMenu_ClickHandler(event:Event):void
            {
                var menuItem:NativeMenuItem = event.target as NativeMenuItem; 

                switch(menuItem.label)
                {
                    case "cut":
                        // do cut operation
                        trace("COMMAND + X");
                        break;
                    case "copy":
                        // do copy operation
                        trace("COMMAND + C");
                        break;
                    case "paste":
                        // do paste operation
                        trace("COMMAND + V");
                        break;
                }
            }

            private function isMAC():Boolean
            {
                return (Capabilities.version.substr(0,3) == "mac");
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
</s:WindowedApplication>

我希望这能解决你的问题...

于 2013-04-02T15:27:49.177 回答