views:

438

answers:

2

Hi, I have a WindowedApplication with a listener on keyboardEvent (on the ENTER key), but when the user choose to use the colorpicker and type an hexadecimal code then hit ENTER the event is propaged to my WindowedApplication too. I have to stop the propagation. Any hint or snippet ? thanks P.

A: 
<?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" 
        keyDown="appHandler(event)">
 <fx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import mx.events.FlexEvent;

   private function appHandler(event:KeyboardEvent):void
   {
    trace('A key has been pressed inside the app');
   }

   private function cpHandler(event:KeyboardEvent):void
   {
    trace('A key has been pressed inside the color picker');
    event.stopPropagation();
   }

  ]]>
 </fx:Script>

 <mx:ColorPicker x="159" y="137" id="cp" keyDown="cpHandler(event)"/>
 <s:TextInput x="233" y="137"/>

</s:WindowedApplication>
99miles
Actually, I have 2 listeners ... one on the app and another on the colorpicker.I've tried using both KeyboardEvent.ENTER and ColorPickerEvent.ENTER
Kangt_qc
Show us some code. An event listener on the colorpicker will not propogate to the WindowedApplication.Sounds like you might have the event listener for the WindowedApplication calling the same method as the event listener for the ColorPicker.
99miles
Ahah! Sorry, I misunderstood. I thought you meant it was being 'triggered' outside of the colorpicker. I updated the code. The key is to add stopPropogration().Note that for the application keyDown handler to be called, the app needs focus somewhere, which you can achieve by placing your cursor in the textInput.
99miles
A: 

This is part of the code of the TitleWindow, where cp is the ColorPicker.

...     

public function init():void { cp.addEventListener(ColorPickerEvent.ENTER,handler); }

public function handler(e:ColorPickerEvent):void { e.stopImmediatePropagation(); }

public function changeColor(e:ColorPickerEvent):void { Application.application.couleur = cp.selectedColor; PopUpManager.removePopUp(this); } ...

And this is from my main mxml :

... employeeList.addEventListener(KeyboardEvent.KEY_UP, enterListener); ...

private function enterListener(e:KeyboardEvent):void {
if(e.keyCode == Keyboard.ENTER) {
if(employeeList.selectedItem) {
showDetail(employeeList.selectedItem as Employee);
}
}
}

Kangt_qc
Did you try my code below? Does it still not do what you want?
99miles
Yes you're right !! using the keyDown properties works like a charm !!thank you.
Kangt_qc
Feel free to 'accept' that answer ;) Thanks.
99miles