[Flex]flex侦听键盘事件代码示例
[Flex]flex侦听键盘事件代码示例,在监听键盘事件之前,让舞台获得焦点是必要的,加一个单击事件来变通一下
在监听键盘事件之前,让舞台获得焦点是必要的,加一个单击事件来变通一下
- <?xml version="1.0" encoding="utf-8"?>
- <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
- xmlns:s="library://ns.adobe.com/flex/spark"
- xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955"
minHeight="600" creationComplete="init()">- <s:layout>
- <s:BasicLayout/>
- </s:layout>
- <fx:Declarations>
- <!-- 将非可视元素(例如服务、值对象)放在此处 -->
- </fx:Declarations>
- <fx:Script>
- <![CDATA[
- import spark.core.SpriteVisualElement;
- import mx.controls.Alert;
- private var sprite:SpriteVisualElement;
- internal function init():void{
- sprite = new SpriteVisualElement();
- sprite.graphics.beginFill(0xFF0000);
- sprite.graphics.drawCircle(10,10,30);
- sprite.graphics.endFill();
- this.addElement(sprite);
- // sprite.
- sprite.addEventListener(MouseEvent.MOUSE_DOWN,startDragHander);
- sprite.addEventListener(MouseEvent.MOUSE_UP,stopDragHander);
- this.addEventListener(KeyboardEvent.KEY_UP,keyHander);
- }
- internal function startDragHander(evt:MouseEvent):void{
- sprite.startDrag();
- }
- internal function stopDragHander(evt:MouseEvent):void{
- sprite.stopDrag();
- }
- internal function doFoucs():void{
- stage.focus = this;
- }
- internal function keyHander(evt:KeyboardEvent):void{
- //Alert.show(evt.keyCode.toString());
- switch(evt.keyCode){
- case 37:
- sprite.x-=10;
- break;
- case 38:
- sprite.y-=10;
- break;
- case 39:
- sprite.x+=10;
- break;
- case 40:
- sprite.y+=10;
- break;
- default:
- break;
- }
- }
- ]]>
- </fx:Script>
- <s:Button x="539" y="369" label="场景获得焦点" click="doFoucs()"/>
- </s:Application>
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...