Home Flex Flex KeyboardEvent Listener
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Flex KeyboardEvent Listener

Advertisement
Example below demonstrates the working of KeyboardEvent listeners.

Flex KeyboardEvent Listener

     

Example below demonstrates the working of KeyboardEvent listeners. In the example when certain specific keys are pressed the function eventListener invokes the functions neo and trinity. These specific keys are shift and ctrl which when pressed together opens a web page url. Also listener for keys are created using conditional statements and with these listeners when ever the boolean condition becomes true, an alert box is generated.

keyListeners.mxml

<?xml version = '1.0'?>
<mx:Application xmlns:mx = 'http://www.adobe.com/2006/mxml' 
  initialize="eventListener()">
  <mx:Script>
  <![CDATA[
  
  public function eventListener():void{
  application.addEventListener(KeyboardEvent.KEY_UP, neo);  
  this.addEventListener(KeyboardEvent.KEY_UP, trinity);
  }
  
  public function neo(eve:KeyboardEvent):void{
  
  var CtrlKey:Boolean = eve.shiftKey;
  if(CtrlKey){
  var ShiftKey:Boolean = eve.shiftKey;
  if(ShiftKey){  
var url_0:URLRequest = new URLRequest("http://www.roseindia.net/flex");
  navigateToURL(url_0);
  }  
  }
  
  }
  
  import mx.controls.Alert;
  public function trinity(eve:KeyboardEvent):void{
txt.text = magic(eve.charCode" (" + String(eve.charCode")" 
+ magic(eve.keyCode" (" + String(eve.keyCode")";
  var power:int = eve.keyCode;  
  
  if(100 < power < 105)
  Alert.show("Welcome to roseindia\n" + txt.text);
  }
  
  
  public function magic(IntVal:int):String {
  if (IntVal > 47 && IntVal < 58) {
  var strIntVals:String = "0123456789";
  return strIntVals.charAt(IntVal - 48);
  else if (IntVal > 64 && IntVal < 91) {
  var strCaps:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  return strCaps.charAt(IntVal - 65);
  else if (IntVal > 96 && IntVal < 123) {
  var strLow:String = "abcdefghijklmnopqrstuvwxyz";
  return strLow.charAt(IntVal - 97);
  else {
  return IntVal.toString();
  }
  }  
  
  
  
  ]]>
  </mx:Script>
  
  <mx:Style>
  .eve {color : #660000}
  TextArea {font-size : 18pt}
  </mx:Style>
  
  
  <mx:Canvas backgroundColor = '#CCFF00' width = '100%
  height = '100%'>
  <mx:TitleWindow title = 'roseindia.net
  showCloseButton = 'true' width = '40%' height = '30%'>
  
 <mx:TextArea id = 'txt' width = '80%
 height = '50%' styleName = 'eve'/>
 <mx:Text id = 'tnt' color = "#CCFFFF"/>
  </mx:TitleWindow>
  </mx:Canvas>
  
</mx:Application>  

 

keyListeners.swf

when the ctrl + shift is pressed following url (http://www.roseindia.net/flex) web page opens in you default browser

Download the code

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 11, 2009

Ask Questions?    Discuss: Flex KeyboardEvent Listener  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com