How to wait for a specific keypress before continuing

K.V.
15 Dec 2017, 20:39

Sherlocho was trying to get this working back in August.

https://textadventures.co.uk/forum/quest/topic/fromceo72e2suyw7en4jbw/possible-to-wait-for-a-specific-keypress-to-continue

Dr. Agon reminded me of this this morning, and mrangel and Pixie have taught me an awful lot about JS since then, so I can make this work now.

<!--Saved by Quest 5.7.6404.15496-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="Continue Function">
    <gameid>176bc4ca-f00d-456f-bfc9-b90bf4452749</gameid>
    <version>1.1</version>
    <firstpublished>2017</firstpublished>
  </game>
  <object name="room">
    <inherit name="editor_room" />
    <description type="string"></description>
    <enter type="script">
      WaitFunction ("g")
    </enter>
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
    </object>
  </object>
  <function name="WaitFunction" parameters="key"><![CDATA[
    key = UCase(key)
    key = Asc(key)
    if (key<Asc("A") or key>Asc("Z")) {
      error ("The key must be a letter!")
    }
    // DEBUGGING
    // msg(key)
    msg ("<div style='display:none' id='pressToContinue'><center><h4>PRESS "+Chr(key)+" TO CONTINUE!</h4></center></div>")
    JS.eval ("$('#pressToContinue').insertBefore($('#txtCommand')).css('display','block');setInterfaceString('TypeHereLabel','Press "+Chr(key)+" to continue!');$('input').bind('keyup',function(e){var value = (e.keyCode);if(value !== parseInt('"+key+"')){document.getElementById('txtCommand').value = '';setInterfaceString('TypeHereLabel','You must press "+Chr(key)+"');}else{	$('input').unbind('keyup');	setInterfaceString('TypeHereLabel','Type here...');$('#pressToContinue').hide();document.getElementById('txtCommand').value = '';}});")
  ]]></function>
</asl>

I've got it calling the function in the room's After enter script, just in case anyone wants to play with this and change it around.

Just be sure to wrap your letter in "" in the parameter, just like you would any other string.

Example:

WaitFunction("c")