Flash AS3: ENTER does not get detected, but CTRL+ENTER works fine

好久不见. 提交于 2020-01-05 12:14:16

问题


When my focus is inside the input text field, pressing CTRL+ENTER works but ENTER does not.

Pressing Enter when my focus is anywhere BUT the input text field works just fine..

My intention is to detect if ENTER key was pressed after the user fills out the field, but it seems to only work for CTRL+ENTER

ActionScript 3:

// works:
stage.addEventListener(KeyboardEvent.KEY_DOWN, enterHandler);

// ignored:
email.addEventListener(KeyboardEvent.KEY_DOWN, enterHandler);

function enterHandler(event:KeyboardEvent):void{
    if(event.keyCode == Keyboard.ENTER ){
        email.text = 'Thanks!';
    }
}

ENTER results in charCode == 0, whereas CTRL+ENTER is charCode == 13

email was created using the Text tool and set to "Editable"

Note: I am testing in Chrome and Firefox running Flash v10


回答1:


i'm assuming you're debugging your work in ADL (Control > Test Movie > in Flash Professional)? the problem here is that the keyboard shortcuts have precedence over keyboard events and the enter key is the keyboard shortcut for Control > Play in the Control menu while you are testing your movie.

however, it's possible and very easy to disable the keyboard shortcuts while testing your movie. when your movie is playing, goto Control > Disable Keyboard Shortcuts. now your keyboard event for the enter key will execute properly.


[EDIT]

oh, and you should use event.keyCode instead of event.charCode.

[EDIT #2]

ok, if you want Enter keyboard event to fire when your currently inside an input TextField, you simply have to add TextEvent listener to the TextField:

import flash.events.TextEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFieldType;

var tf:TextField = new TextField();
tf.border = true;
tf.multiline = true; //Must be set to true for the textField to accept enter key
tf.type = TextFieldType.INPUT;
tf.width = 200;
tf.height = 20;

tf.addEventListener(TextEvent.TEXT_INPUT, keyboardReturnHandler);       

function keyboardReturnHandler(evt:TextEvent):void
    {
    if  (evt.text == "\n")
        {
        evt.preventDefault();
        trace("text field enter");
        }
    }

addChild(tf);


来源:https://stackoverflow.com/questions/6462028/flash-as3-enter-does-not-get-detected-but-ctrlenter-works-fine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!