Форум Flasher.ru

Форум Flasher.ru (http://www.flasher.ru/forum/index.php)
-   ActionScript 3.0 (http://www.flasher.ru/forum/forumdisplay.php?f=83)
-   -   Нажатие кнопки, при нажатии кнопки на клавиатуре (http://www.flasher.ru/forum/showthread.php?t=214858)

саша55555 25.12.2017 17:52

Нажатие кнопки, при нажатии кнопки на клавиатуре
 
У меня на кадре есть кнопка "button1". Как сделать, чтобы при нажатии на клавиатуре кнопки ENTER, нажималась эта кнопка?

caseyryan 25.12.2017 18:05

Повесь их на один обработчик, а как аргумент принимай просто Event или Object
Код AS3:

button1.addEventListener(MouseEvent.CLICK, onButton);
stage.addEventListener(KeyboardEvent.KEY_UP, onButton);
 
function onButton(e:Event):void {
    if (e is MouseEvent) {
          if (e.target == button1) buttonAction();
    } else if (e is KeyboardEvent) {
        var keyboardEvent:KeyboardEvent = e as KeyboardEvent;
        if (keyboardEvent.keyCode == KeyCode.ENTER) {
            buttonAction();
        }
    }
}
function buttonAction():void {
    // код, который должен выполняться по нажатию нужной кнопки
}


саша55555 25.12.2017 18:12

А какой код для нажатия кнопки?

undefined 25.12.2017 18:14

это тебе виднее

саша55555 25.12.2017 18:19

Нет, мне нужно чтобы при нажатии на ENTER выполнялось действие "нажать на кнопку", про ENTER я понял, но какой код нужен, для нажатия на кнопку "button1"?
Код AS3:

function buttonAction():void {
    // код, который должен выполняться по нажатию нужной кнопки
}


ZergMaster 25.12.2017 18:36

Код AS3:

button1.addEventListener(MouseEvent.CLICK, buttonHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyBoardHandler);
 
function keyBoardHandler(event:KeyboardEvent):void
{
        if (event.keyCode == KeyCode.ENTER)
            buttonHandler();
}
 
function buttonHandler(event:MouseEvent=null):void
{
        trace('кнопка нажата');
}


undefined 25.12.2017 18:39

Цитата:

но какой код нужен, для нажатия на кнопку "button1"?
Код AS3:

button1.dispatchEvent(new MouseEvent(MouseEvent.CLICK));


саша55555 25.12.2017 18:44

Помогите, при нажатии на Enter все работает как надо, но в лог вылетает
Код AS3:

Error: Error #2094: Переполнение при рекурсивной передаче события.
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at test_fla::ChatPanel_26/buttonAction()
        at test_fla::ChatPanel_26/onButton()


ZergMaster 25.12.2017 19:16

так мы ж не знаем, что вы делаете

undefined 25.12.2017 19:36

Цитата:

button1.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
этот код нажимает на кнопку без клика по ней.Естественно, если засунуть его в обработчик нажатия все зациклится


Часовой пояс GMT +4, время: 09:48.

Copyright © 1999-2008 Flasher.ru. All rights reserved.
Работает на vBulletin®. Copyright ©2000 - 2026, Jelsoft Enterprises Ltd. Перевод: zCarot
Администрация сайта не несёт ответственности за любую предоставленную посетителями информацию. Подробнее см. Правила.