0

I would like to get my page to read keystrokes wherever the person is focused on the website.

So like if they had the web page open and they pressed 1 then it did something and if they pressed 2 it did something else and so on. I don't mean to do this in an input tag.

How would I go about doing this? Can PHP just catch the keystroke and do something or does it have to respond back with AJAX? If so, how?

rsjaffe
  • 5,600
  • 7
  • 27
  • 39
Hyperion
  • 869
  • 1
  • 8
  • 23
  • PHP is `ServerSide`! You have no access on `ClientSide` Keystrokes. Else, try to use JQuery for this: http://api.jquery.com/event.which/ If you wanna execute PHP Functions on Keystrokes, catch the KEYCODE (like "2", or sth. else and send an `AJAX Request` to the PHP Function with the keycode) – Tyralcori Aug 14 '14 at 06:02
  • 1
    See http://stackoverflow.com/questions/4532773/how-do-i-capture-keystrokes-on-the-web – rurouni88 Aug 14 '14 at 06:03

1 Answers1

1

You'll need to listen to a keyboard event with JavaScript.

PHP is a server side language and cannot help you for this.

Simple demo over here: http://jsbin.com/lamow/1/edit

document.addEventListener('keypress', function(event) {
  console.log(arguments);
});
Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134