1

I am making a simple text adventure using javascript and I need a way to make the player be able to write 1, 2, 3 or 4 when asked in order to select the corresponding choices. How do I do this? readline() and console.read() don't work.

I am not using a website. I need it in the console.

5
  • 1
    Forget about the console... Use window.prompt(). Commented Jun 20, 2013 at 21:58
  • 1
    stackoverflow.com/questions/4416505/… Commented Jun 20, 2013 at 21:59
  • If this is in a browser just use a textbox... Commented Jun 20, 2013 at 22:00
  • You can also use a standard text input, or a jQuery dialog, or an event listener, etc... Commented Jun 20, 2013 at 22:01
  • You can always run it on jsfiddle.net and use the tools previously suggested. Commented Jun 20, 2013 at 22:53

1 Answer 1

0
document.addEventListener('keydown', function(event) {
    if(event.keyCode == EVENTCODE) {
        //action
    }

});

in the place of EVENTCODE place numbers:

48 = 0
49 = 1 
50 = 2 
51 = 3 
52 = 4 
53 = 5 
54 = 6 
55 = 7 
56 = 8 
57 = 9,
Sign up to request clarification or add additional context in comments.

5 Comments

Why not just finish the example by adding the numbers (at least partially)?
How am I supposed to use it if I have no document? I am not using a website for this.
How am I supposed to know that?
@user1571481: so in what environment is this running? Did you not think that information might be of use to the people trying to help you out?
@David Thomas I want to run it in repl.it javascript interpreter

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.