flex 裡鍵盤事件

來源:互聯網
上載者:User

一、

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
      addedToStage="stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown)"   
     click="clickEvt(event)"
     layout="absolute"
     frameRate="6"
     creationComplete="start()" keyFocusChange="testKey(event)"    
  backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#990000, #FF9900]">
 <mx:Script source="SnakeMain.as"/> 

二、<?xml version="1.0" encoding="utf-8"?>  
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
                layout="absolute" 
                applicationComplete="appComplete();">  
    <mx:Script>  
       <![CDATA[  
           import mx.controls.Alert;  
            import flash.events.KeyboardEvent;  
              
            //監聽keyboard事件  
            private function appComplete():void  
            {  
                 this.addEventListener(KeyboardEvent.KEY_DOWN,keyHandler);
                //this.addEventListener(KeyboardEvent.KEY_UP, myKeyUp);  
                //this.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);  
            }  
               private function keyHandler(event:KeyboardEvent):void
               {
               trace(event.charCode);
               }
            //響應keyup事件  
            private function myKeyUp(evt:KeyboardEvent):void  
            {  
                mx.controls.Alert.show('evt.keyCode=' + evt.keyCode.toString()+"\n"+ "彈起\n", "KeyUp");  
            }  
              
            //響應keydown事件  
            private function myKeyDown(evt:KeyboardEvent):void  
            {  
                t1.text=t1.text + 'evt.charCode=' + evt.charCode + ' evt.keyCode=' + evt.keyCode + ' evt.keyLocation=' + evt.keyLocation + " 按下\n";  
            }  
        ]]>  
    </mx:Script>  
    <mx:TextArea x="261" 
                 y="226" 
                 id="t1" 
                 width="381" 
                 height="200"/>  
    <mx:TextInput x="372" 
                  y="114" 
                  id="t2"/>  
</mx:Application> 
當你把所有方法寫在一個as檔案裡時用第一種方法擷取鍵盤事件

當在mxml頁面裡擷取鍵盤事件時用第二種方法!!!!!

 不管怎麼去處理,都要找准給誰添加鍵盤監聽事件 ok 這樣就哦了!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.