Hello!
I'm using Twine 2 with Sugarcube 2 to build a game centered around text input.
I want to be able to have players submit text with the enter key.
Currently i'm doing text submit with a custom parsing macro + goto wrapped in a <<click>> macro. I need to use a textArea because double checking text is important to the game, and the amount of text people usually write has to use word wrap to display in full. (I've yet to see someone intentionally use a newline, so losing it won't be a big deal.)
Is there a way to run script(s) or forward to another passage on an enter keypress with a textArea in focus? Currently my text area + button for submitting text and progressing to the next passage looks like this:
<<textarea "$variables.answers[0]" $variables.boxText autofocus>>
<<click "SUBMIT ANSWER">><<ruleCheck $variables>><<goto passage02>><</click>>
Ideally, I'd like it to run <<ruleCheck $variables>> and <<goto passage02>> on keypress, but if I could just get a passage forward or a single macro to activate on an enter keypress, I can work around that.
Comments
In story javascript write:
Then you would change your click macro to:
This will convert the click macro within the textenter span to also activate on enter.
If you want to hide the click macro to just leave enter activation, you can use css to set visibility or display to hidden for links of the ".textenter a" class, and the enter key will still work.
I haven't tested this to see if the focus area changes whether this will work, though. I'm just guessing it won't since it just searches for all links.
Thanks! I'll try this out tonight and tell you how it goes
Beyond that, if you're only going to use it for this purpose, then something similar to, but not exactly, what Claretta outlined should work fine.
I would suggest the following for your <<textarea>>/<<click>>: And the following to enable the functionality you want: (goes in Story JavaScript)
PS: If you wanted to use a <<button>> instead of <<click>>, which is more common for this sort of thing, then simply replace the <<click>> with the <<button>> and change ".macro-click" to ".macro-button" in the script.
That looks like exactly what i need, thanks! And- thanks in general for all the questions you answer on these forums. Your posts have really helped me out, especially since I'm stretching the intended purpose of Twine pretty far on my first game.
What's the benefit of using "button" over "click"?
I only mentioned it because, as I said, it's more common to pair an input element with a <<button>>—probably because that's what you do in HTML. There's nothing inherently wrong with using <<click>> if that's your preference.