• Question: Can you make Javascript respond to keys, for instance if I pressed 'w' could it execute an if statement? Also, how long have you been interested in programming?

    Asked by DrProfessorKit to Andy on 17 Dec 2014.
    • Photo: Andy Hearn

      Andy Hearn answered on 17 Dec 2014:


      http://javascript.info/tutorial/keyboard-events
      I’d go for the keyup event to run your ‘w’ statement/script.
      If we went for the keydown event, we’d need to add some kind of checking to only run the script once for a long keypress (what would the code do for an input of “wwwwwwwwww” ?)

      Other JavaScript stuff and guidelines can be found on the same site: http://javascript.info/tutorial (and many more sites out there) – happy reading!

      I’ve been interested in programming ever since my grandfather bought a Sinclar ZX Spectrum 48K (people of my age have fond memories of these low-spec computers – some programs took 4 minutes, yes that’s right, four whole minutes to load!)
      I think the ‘ah-ha’ moment for me was when I spotted a relationship between a line of code and how the output is generated, and built up my understanding from there on!

      So that’s nearly 30 years – I’m shocked now… 🙂

Comments