1 var v:Canvas = new Canvas();
2 v.addEventListener(KeyboardEvent.KEY_DOWN,OnkeyDown);
3 private function OnkeyDown(e:KeyboardEvent):void{
4 Alert.show(e.keyCode.toString());
5 }
2 v.addEventListener(KeyboardEvent.KEY_DOWN,OnkeyDown);
3 private function OnkeyDown(e:KeyboardEvent):void{
4 Alert.show(e.keyCode.toString());
5 }
看了上面的代码感觉应该可以了的 其实进行编译根本不行
问题就在于 当canvas 没有得到焦点的时候是无法响应键盘事件的
则改成如下就成功了
1
var v:Canvas = new Canvas();
2
v.addEventListener(KeyboardEvent.KEY_DOWN,OnkeyDown);
3
v.addEventListener(MouseEvent.MOUSE_UP.OnMouseUP);
4
private function OnkeyDown(e:KeyboardEvent):void{
5
Alert.show(e.keyCode.toString());
6
}
7
private function OnMouseUP(e:MouseEvent):void{
8
(e.currentTarget as Canvas).setFocus();
9
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)