0

let me clear something up real fast- I'm not making a keylogger. I'm creating a site for a video game where users gain items for doing specific actions in game (such as typing something in chat, etc.).

Because of the way this game works, I feel the best way to do it would be to have a website that would, while open, record everything typed (and negate backspaces) to see if the player had completed the action. Then it would lead them onto the next action.

Is there a way to have a PHP or JavaScript script running inside of the website that would record all keystrokes that are typed while the page is open? Then I can have it just listen until it reads the player execute the command (such as joining a guild or trading somebody)?

-Vivian

Lauren Hinch
  • 340
  • 1
  • 11
  • Slightly tangential, but very important: PHP runs server-side, JS runs client side. PHP never runs on your site, it is used by the server to generate responses to your user's requests. You would use JS to capture their keystrokes and maybe (depending on your app) send those strokes to your server for storage or additional processing. – The Unknown Dev Oct 24 '16 at 00:14

2 Answers2

0

Yes, its very easy to accomplish something like that with jQuery. Once you have included the lib, you can put certain events on elements. A way to set a key listener event, when the user releases the key would be something like this:

$('body').keypress(function (event) {
    var character = String.fromCharCode(event.charCode)
    alert(character);
});

You can then send the result to php via AJAX (which you can also do pretty easy in jQuery (put this in a separate function and call the function in your keypress function):

var url = 'your_script.php?character=' + character;
$.get(url, function (data) {
    alert(data); // alert response from your_script.php
});
Manuel Mannhardt
  • 2,191
  • 1
  • 17
  • 23
  • There is no good reason to suggest a library for this when all it takes is a single listener. Also, [*event.which*](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/which) is a deprecated feature, so not a good to suggest it. – RobG Oct 24 '16 at 00:53
  • Since he also wants to use the key inside PHP, I do think its a good reason to use jQuery. Why should he reinvent the wheel, when there is already a good solution? He probably will write more JavaScript code, so thats about that. Also jQuery has support for all browser already implemented. Thanks for the information, I changed my code. – Manuel Mannhardt Oct 24 '16 at 01:00
  • The issue is that there are [*many libraries and frameworks*](https://www.smashingmagazine.com/2009/03/40-stand-alone-javascript-libraries-for-specific-purposes/) that can be used to implement functionality, there is no point in say 6 answers with the same solution just different libraries. If no specific library is mentioned, it's better to just show a plain JS solution and let the OP work out which library or framework to implement it with. Or show both plain JS and a library. In this case a plain JS solution is no more code than a library. – RobG Oct 24 '16 at 01:44
  • You might be right sir. Well looks like OP doenst even care anymore, so I will leave my answer for further searchers. Cheers – Manuel Mannhardt Oct 24 '16 at 15:45
0

You can find the key pressed with javascript. If they're in an input field you could have a function that checks the value of the field on key press also.

This post here answers it.

How to find out what character key is pressed?

Community
  • 1
  • 1
milkcup
  • 55
  • 6