Jump to content

Onkeydown


driz

Recommended Posts

I'm trying to create a page that will use PHP to detect a keydown so for example if a user is on my page and types ALT + A it will do a function.Is this possible? I don't want to use JS as JS can be viewed in the source, and I don't it to be viewed! Thanks

Link to comment
Share on other sites

It is certainly not possible. PHP runs on your server, not in the browser. The only way PHP could respond to a browser event is if the browser communicated the event to PHP. It could do this through a link, a form, or AJAX. Either of these methods would be quite visible to your user. And since you're talking about key events, the waiting time would be ANNOYING.A lot of user interaction HAS to take place on the browser. In fact, the trend these days is to INCREASE the amount of JavaScript programming in browser documents. You just have to live with the idea that techniques and code will be borrowed.Considering the fact that PHP comes to you FREE, and your browsers come to you FREE, it's really not a bad deal.

Link to comment
Share on other sites

Then use the keystroke to send an ajax request to run the PHP code. PHP can't detect browser events, and Javascript can't run on the server. You need both.
How do I do that? I have read about something called keycodes (are they needed or just another way) but I don't understand how to grab em and then send to php.To break it down: User types SHIFT + ~ and then the php runs a function like:
function keyRedirect() { header("Location:http://auth.mydomain.com/login.php"); }

Edit: The key combination can't be seen at all in the JS, it would have to be done all inside PHP otherwise someone could see what the combination was to access the pageSo JS basically just grabs the keystrokes and fires them off to PHP and that's all. The PHP checks the key strokes and if they match those mentioned then they run the function.

Link to comment
Share on other sites

Yes but I don't understand how to use it with keystrokes and use it with a PHP function that is looking for that particular key stroke.
  1. Javascript detects the key.
  2. Based on the key that was pressed, it send a specified request to the server with AJAX.
  3. The server runs a script and gives a response.
  4. Javascript does something with the response.

Link to comment
Share on other sites

PHP isn't looking for a keystroke. Here's your basic flowchart.

  1. User hits a key.
  2. A handler in javaScript is triggered by the keystroke.
  3. The handler sends an AJAX message to the server. The message contains information related to the keystroke.
  4. The PHP script that got contacted responds in a way that matches the key stroke.
  5. Javascript does something with the response that comes from the server.

Link to comment
Share on other sites

Thanks for all the info, but I don't know how to do that.
I was under the impression that you had used AJAX before, so I thought you'd understand that.You should try looking through the W3Schools AJAX tutorial to learn how to send requests to the server and use the response that the server sends back.
Link to comment
Share on other sites

You need a keydown handler, probably attached to the window so you catch all keystrokes. You've probably written one of these:window.onkeydown = //somethingMostly you need to learn AJAX. Yes, it's a steep learning curve.If you're already scripting in PHP or ASP or something, then there won't be much difference.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...