Flex Canvas键盘事件处理

时间:2010-10-23 10:46:42

标签: flex actionscript-3 keyboard-events

在附带的代码示例中,键盘事件不起作用 无论我按哪个键,控件都不会到达 handleKeyDown

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Canvas creationComplete="initIt()" id="can1">
 <mx:Label text="it's here" y="2000"/>
</mx:Canvas>
 <mx:Script>
  <![CDATA[

   private function initIt():void {
     can1.addEventListener(KeyboardEvent.KEY_DOWN,handleKeyDown); 
   }

   private function handleKeyDown(e:KeyboardEvent):void {
    if(e.keyCode == Keyboard.UP) {
     trace("up");
     verticalScrollPosition++;
    }
    if(e.keyCode == Keyboard.DOWN) {
     trace("down");
     verticalScrollPosition--;
    }    
   }
  ]]>
 </mx:Script>
</mx:Application>

2 个答案:

答案 0 :(得分:0)

原因是通常Canvas没有焦点。没有焦点,通常没有键盘事件。请查看“Problem with handling keyboard events inside a Canvas using Flex.”以获得可能的解决方案。

答案 1 :(得分:0)

尝试在initIt()函数中手动切换焦点