`
blueram
  • 浏览: 763968 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

flex捕获键盘长按事件

    博客分类:
  • Flex
 
阅读更多

项目需要,在flex air中对长按键做出处理,我是这样定义长按键的。

当键盘按下时记录按下状态isPressed=true;

键盘弹起时isPressed=flase;

添加计时器,长按5s,触发处理事件。

 

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:utils="com.plter.air.windows.utils.*" keyDown="onKeyWown(event)" keyUp="onkeyup(event)">
	
	
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import com.plter.air.windows.utils.ShutdownCommand;
		
			private var keycount:int = 0;
			private var isPressed:Boolean = false;
			private var timer:Timer = new Timer(5000, 1);
			
			protected function onKeyWown(event:KeyboardEvent):void
			{
				trace("downKey:" + event.keyCode);
				//Alert.show("downKey:" + event.keyCode);
				isPressed = true;
				if(event.keyCode == 13 && keycount == 0) {					
					timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
					timer.start();							
				}				
				keycount = keycount+1;
			}
			
			function onTimerHandler(event:TimerEvent):void{
				trace("on timer");
				if(keycount > 30 && isPressed){
					trace(keycount);
					trace("关机");
					//shutdown.shutdown(3);
				} else {
					trace("长按5s关机")
					
				}
				keycount = 0;
			}
			
			protected function onkeyup(event:KeyboardEvent):void
			{				
				trace("upKey:" + event.keyCode);				
				isPressed = false;		
				timer.stop();
				trace("timer stop:" + keycount);
				keycount = 0;
			}
			
		]]>
	</fx:Script>
	
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
		
		<utils:ShutdownCommand id="shutdown"/>
		
		
	</fx:Declarations>
	<s:Label width="70" height="20" horizontalCenter="-63" text="按键信息:" verticalCenter="-39"/>
	<s:Label id="msg" x="583" y="182" width="244" height="51" text=""/>
	<s:Button x="290" y="112" label="按钮" />
</s:WindowedApplication>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics