Hyperion
Hyperion

Reputation: 909

Reading keystrokes in PHP or AJAX

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?

Upvotes: 0

Views: 657

Answers (1)

Simon Boudrias
Simon Boudrias

Reputation: 44649

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);
});

Upvotes: 1

Related Questions