开发者

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

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(Keyboard开发者_如何学JAVAEvent.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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜