0

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 e.g. 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);
cubeec
  • 15
  • 4
  • If you're trying to read what characters are entered into a textfield you could use the [textInput](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html#event:textInput) event. It has a text property that holds which characters were entered. I don't know if you could get character codes out of it to identify if the enter key was pressed or not, but you could certainly use that to compare it with a 'correct answer' and see if it matches. Alternatively you could simply read the text property of the text field after it had received input. – Garry Wong Jun 07 '13 at 15:42
  • @GarryWong Thanks. I'll check that. I'm using [native text imput with stage text](http://blogs.adobe.com/cantrell/archives/2011/09/native-text-input-with-stagetext.html) and I'm a bit struggling with the textInput event. – cubeec Jun 07 '13 at 16:05

2 Answers2

0

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'");
        }
    }
}
Prankard
  • 19
  • 1
  • I've tried this and it doesn't work. I'm getting this error: Line 113 1119: Access of possibly undefined property text through a reference with static type NativeText. I'm sure it has something to do with the native text class I'm using but I have no idea how to fix it. In case you want to see the class: [native text imput with stage text](http://blogs.adobe.com/cantrell/archives/2011/09/native-text-input-with-stagetext.html) or here on [github](https://github.com/cantrell/StageTextExample). Thanks a lot! – cubeec Jun 08 '13 at 14:45
0

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"...

RYan
  • 51
  • 2