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