Textbox Output Merging words

Description of the problem:

Hi,

I’m coding a free-recall memory experiment where participants type words into a TextBox component.

What I’m hoping to achieve

  • Participants should be able to enter multiple words, either by pressing space or pressing enter to move to a new line.
  • In the csv output file, words should be stored separately (e.g. “dog cat bird” or [“dog”, “cat”, “bird”)

Problem
Currently, if participants press ‘enter’ between words instead of space, the output is saved as one continuous string without spaces (e.g. “dogcatbird” instead of “dog cat bird”).

I’m not that comfortable or familiar with Javascript. However, I did try using the following piece of code to split by spaces and newline characters;

user_answer = textboxRetrieval.text.split(/\s+/);

Unfortunately, this did not work.

Question
Is there a way to handle both spaces and enter presses, or is this just something that I will need to tackle when it comes to cleaning the data?

I’m not 100% sure how the text box actually records “enter” presses, but if they are standard newline symbols, you could try this:

user_answer = textboxRetrieval.text.split(/[\s\n]+/);

Square brackets mean “one of these”, so it should split on either a space or a line-break.

Thank you for the suggestion!

Unfortunately, it doesn’t appear to fix the issue.

I used console.log(JSON.stringify(user_answer)) to see whether the textbox is recording “enter” presses as newlines, and it does. Therefore, I’m not completely sure why splitting does not work?

Interesting. How does it look if you do console.log(textboxRetreival.text) instead? The same?

I have an idea to solve this but I haven’t had a chance to test it yet.

Add a keyboard component that monitors for return and Each Frame code which adds a space at or near the end of textbox.text when a new return is pressed.

This turned out to be significantly harder to solve than I was expecting, but my final solution is more elegant than the one I had to abandon.

What I discovered is that textbox.text is not the same during the routine as when it ends. That’s the point at which the newline characters get stripped out. My first attempt was fine for every word apart from the last one, where it lost most or all of the letters.

My tip for today is how to define a split function so you can use an Auto code component in the trial routine, and how to call the split function as the routine is ending, before the newline characters have been stripped out.

1 Like

Thank you for taking the time to look into this. This solution worked perfectly!