views:

27

answers:

2

In the attached code sample, keyboard events just don't work.
The control never reaches handleKeyDown, no matter which key I press.

<?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>
A: 

The reason for that is that normally a Canvas doesn't have focus. Without focus there normally are no keyboard events. Thake a look at "Problem with handling keyboard events inside a Canvas using Flex." for a possible solution.

Gerhard
A: 

try to switch focus manually in initIt() function

www0z0k