简体   繁体   中英

AS3 iOS 'catch'/'recognise' that user pressed return key on the soft keyboard

I was trying to find a solution online but couldn't find a proper answer. Does anyone know how to 'catch'/'recognise' that user pressed return key on the soft keyboard (iOS)? And how to check if the input equals to eg correct answer?

Thanks a lot.

My code:

nt = new NativeText(1);
            this.nt = nt;
            this.nt.returnKeyLabel = ReturnKeyLabel.DONE;
            this.nt.autoCorrect = true;
            this.nt.fontSize = 40;
            this.nt.borderThickness = 1;
            this.nt.fontFamily = "Arial";
            this.nt.text = "pica";
            this.nt.color = 0xFFFFFF;
            this.nt.borderColor = 0xFFFFFF;
            this.nt.width = 500;
            this.nt.x = 70;
            this.nt.y = 70;

            LEVEL_02_STAGE.addChild(this.nt);

You can't pull much data from user keystrokes in iOS. But you can pull the enter command from the KeyboardEvent if the keycode is 13.

So if you had a text input already defined as 'var textfield:Textfield;'

textfield.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownPress);

function onKeyDownPress(e:KeyboardEvent):void
{
    if (e.keyCode == 13)
    {
        // Return key (done in iOS is pressed)
        if (textfield.text == "equalstothisstring")
        {
             trace("The input is valid and equals to 'equalstothisstring'");
        }
    }
}

I want to point out something misleading in this post.

on iOS, Return is not the same thing as Enter.

If you make a multiline textfield, give it focus, thekeyboard will say "Return" instead of "Done".... the keyboard event will only fire for "Done" and NOT for "Return"...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM