How to make a button not shoot at a space if it has focus?

The behavior I'm trying to prevent is when the user presses the button, the button retains focus, and then, if the user presses the space bar, the button fires again. Therefore, I decided that this can be solved either by setting the focus in another place, or by pressing the space bar. I tried adding the following keyDown key event listener, but that didn't work.

private function btn_keyDown(event:KeyboardEvent):void {
  // try to ignore spaces, i.e. don't click on SPACE when it has focus
  if (event.keyCode == Keyboard.SPACE) {
  }
}

I tried to change the focus by doing the following, at the end of the function called when the button was clicked:

stage.focus = parent;

but that didn't work either.

+3
source share
5 answers

keyDownHandler. , (), , . :

package Sandbox
{
    import mx.controls.Button;
    import flash.events.KeyboardEvent;

    public class KeyButton extends Button
    {
        public function KeyButton()
        {
            super();
        }

        protected override function keyDownHandler(e : KeyboardEvent) : void {
            if (e.keyCode == 32) { // Spacebar
                return;
            }
            else if (e.keyCode == 67) { // Letter C
                this.parentApplication.setStyle ("backgroundColor", "#00aa00");
            }

            super.keyDownHandler (e);
        }

    }
}

, , Enter, , , , , C - .

, KeyboardEvent.keyCode charCode. keyCode - , , c C (keyCode == 67). charCode, , ASCII, c C (C - 67, c - 99). keyCode .

, . char ( , Backspace), Adobe.

, . Flex Builder, src, . , . , . , "", , ! c, . , ??

.

+4

, , .

, , focusEnabled FALSE, ( ) .

+7

event.stopPropogation() "if"

+1

, , . , , , - , .

private var spacePressed:Boolean = false;

private function onKeyDown(event:KeyboardEvent):void {
    if (event.keyCode == Keyboard.SPACE) {
        spacePressed = true;
    }
}

private function onKeyUp(event:KeyboardEvent):void {
    if (event.keyCode == Keyboard.SPACE) {
        spacePressed = false;
    }
}

private function doSomething():void {
    if (spacePressed)
        return;
    // Normal handling...
}

 

<mx:Button label="Button"
    keyDown="onKeyDown(event);"
    keyUp="onKeyUp(event);"
    click="doSomething();"/>
0

The space button is handled inside the button keyDownHandler, so just redefine it with an empty body, and this will solve the problem. Like this:

package test
{
import mx.controls.Button;
import flash.events.KeyboardEvent;

public class NoSpaceButton extends Button
{
    public function NoSpaceButton()
    {
        super();
    }

    override protected function keyDownHandler(event:KeyboardEvent):void
    {
    }

}
}
0
source

Source: https://habr.com/ru/post/1710277/


All Articles