开发者

Flex Canvas keyboard event handling

开发者 https://www.devze.com 2023-01-21 06:11 出处:网络
In the attached code sample, keyboard events just don\'t work. The control never reaches handleKeyDown, no matter which key I press.

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");
     verticalScro开发者_StackOverflow社区llPosition--;
    }    
   }
  ]]>
 </mx:Script>
</mx:Application>


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.


try to switch focus manually in initIt() function

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号