keyPress Duplication Error

I have a text box [$w(“#textBox3”)] that the user can enter text into and when pressing “Enter” the text box should be cleared and the entered text should be pushed in $w(“#text1”). This all works fine however even though the text box is cleared if the user presses “Enter” again it pushes the same value into $w(“#text1”). This does not happen if done with a button.
Note: There are extra lines of code to stop empty values being sent.

Here is the code:

let Input1;
export function textBox3_keyPress(event) {
 //Add your code for this event here: 
 if(event.key === "Enter"){
        event.key = "";
        event = "";
        Input1 = $w("#textBox3").value
        $w("#textBox3").value = "";
        let i;
        for (i = 0; i < Input1.length; i++) {
            if (Input1[i] === '\n' || Input1[i] === '\r') {
                console.log("Enter")
            } else {
                 if (Input1[i] === ' ') {
                    console.log("Space")
                } else {
                    console.log("Text")
                    i = Input1.length;
                    console.log("Input: ", Input1)
                    $w("#text1").text = $w("#text1").text + " " + Input1;
                    Input1 = "";
                    $w("#textBox3").value = "";
                } // End of Input[i] if Space
            } // End of Input[i] if Enter
        } // End of for loop
    } // End of if
    $w("#textBox3").value = "";
    Input1 = "";
 
}

Does anyone know how to make it only send the value on the first press of “Enter” and not the second?

Hello!

It seems that somewhere in your code’s logic it’s allowing the value to be stored and sent to the text element every time the Enter key is pressed. However, when I tried recreating the issue on my end I wasn’t able to get your code to work for me.

I was able to rewrite the code for the functionality you wanted and got it working. Maybe this can help you out:

export function textBox1_keyPress(event) {
 if(event.key === "Enter"){
        $w("#text1").text = $w("#textBox1").value;
        $w("#textBox1").value = "";
    } 
}

Best regards,
Miguel

Thank you for the code but it is too simple and still has the same error as mine; when hitting Enter for the second time it still passes on the previous value. I need to make sure that the value of the textbox is not passed on if it just contains spaces or breaks. It also only happens when triggered by Enter rather than a button so I have no idea where it could be storing the value.

As you are using onKeyPress here, you might want to look at giving the user input a little bit of extra time as well.

See this previous forum post here for more info about it.
https://www.wix.com/corvid/forum/tips-tutorials-examples/give-the-textinput-onkeypress-function-some-time