3 Replies Latest reply on Sep 12, 2007 6:41 AM by levancho

    get Buttons to respond to ENTER key

      What's the best way to get a button to respond to the ENTER key (when the given button has focus)?
      As of now I'm checking the ID of object that has focus (in the main application), and it seems to work fine, as below:

      private function goKey(event:KeyboardEvent):void {
      . if (event.keyCode == Keyboard.ENTER) {
      . . var iF:Object = getFocus();
      . . if (iF.id == "myButton") {
      . . . // execute code here
      . . }
      . }

      ... but I'm wondering if there's a more elegant way to do it, which will open up my understanding of Flex.
        • 1. Re: get Buttons to respond to ENTER key
          just use the "enter" event.
          Here is some AS3 code as example:
          private function myButtonClicked(event:Event):void{
          // execute code here

          best regards,
          • 2. Re: get Buttons to respond to ENTER key
            JKohn99 Level 1
            There are probably lots of ways to handle this. I typically add my keydown
            handler on the container that holds the buttons
            then in the event handler look at the event.target. It its a button then
            dispatch a mouse click event. Then the click event handler for the button
            will be called as if the user clicked it with a mouse.
            just do this:

            private function handleKeyDown(event:KeyboardEvent):void
            var char:uint = event.keyCode;
            var enterChar:uint = 13;
            var tabChar:uint = 9;
            var pgUp:uint = 33;
            var pgDown:uint = 34;
            if(char == enterChar)
            if(event.target is Button)
            var bt:Button = event.target as Button;
            if(bt.enabled == false)
            var c:MouseEvent = new MouseEvent(MouseEvent.CLICK,true);
            • 3. Re: get Buttons to respond to ENTER key
              levancho Level 3
              there is also a short "super-easy "way of doing it with defaultbutton property of a container that has that button in it .all you do is this defaultButton="{your_button_id}" everything else is taken care of automatically..