Flex 3: Key press combination to trigger an event/function

空扰寡人 提交于 2019-12-14 02:43:07

问题


Within a specific canvas, I would like a user to be able to press a combination of keys which will trigger an event.(a bit like a cheat in an old megadrive game). Not sure where to start though. Anyone know if it is possible and if so could you give me a clue with how to start?

Thanks in advance!


回答1:


You can add an eventListener to the top level application for the KeyboardEvent.KEY_DOWN event and check for key combinations there. From this article:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
 layout="absolute" creationComplete="init()">
 <mx:Script>
 <![CDATA[
    private function init():void{
     this.addEventListener(MouseEvent.CLICK, clickHandler);
     this.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
    }
    private function clickHandler(event:MouseEvent):void {
      stage.focus = this;
    }
    private function keyPressed(evt:KeyboardEvent):void{
       if(evt.ctrlKey && evt.keyCode == 65)
             trace("CTRL A is pressed");
       if(evt.ctrlKey && evt.keyCode == 66)
             trace("CTRL B is pressed");
   }
 ]]>
 </mx:Script>
</mx:Application>



回答2:


A canvas won't dispatch key up or key down events. You can add a listener to them; as the key events will bubble; but it won't dispatch them alone. Unfortunately, an input component, such as a textInput, needs to have focus for the key press events to dispatch.

Instead of using a canvas, group, or other container, I'd look into using a Spark TextInput with a customized skin that makes it, and the typed text, essentially invisible.



来源:https://stackoverflow.com/questions/6202633/flex-3-key-press-combination-to-trigger-an-event-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!