1

Hi I got a question that I would like to get some help with. First I was wondering how to detect which key that was pressed and then after that send the key that was pressed to another page or more or less another file that I have on my computer ? Not redirecting I already know how to do that, but more like send the keypress to a "result file" or something like that. So far I got a keypress that redirects to next page/file and when the user presses the button it logs the charCode to the console. But I want it to send the key to another file instead of sending it to the console. Here is my code so far:

jQuery:

document.onkeypress = function(b) {
         b = b || window.event;
         var charCode = b.charCode || b.keyCode,
         character = String.fromCharCode(charCode);

console.log(charCode);
window.location.href="Test3.html";
};

And I got two of those b and r. If there's a possible way to do it in jQuery or JavaScript it would be awesome, but if there's another way to do it in another language I need a detailed description.

Nikki
  • 301
  • 1
  • 2
  • 18

2 Answers2

1
window.location.href="Test3.html/?charCode=" + charCode;

This will give your page 'Test3.html' access to the GET parameter 'charCode'. Though you'd probably want to use a server-side language to do something useful with it, since IMO it is a bit of a hassle to manipulate GET parameters from javascript.

Raphael Serota
  • 2,157
  • 10
  • 17
  • I tried this one to and it didn't make any differens from the original code, thanks anyway ! – Nikki Jan 26 '15 at 09:36
0

PHP

  <script>
     $(document).keypress(function(event){
     var key = event.keyCode;
  </script>
  $.ajax({
            type: "POST",
            url: "<?php echo myphp.php ?>",
            data: {
                'events': key
            },
            success: function(data)
            {
            },
            complete: function()
            {        
            }
        });  

myphp.php

 $key = $_POST['events'];
 $myfile = fopen("filename.txt", "w") or die("Unable to open file!");
 fwrite($myfile, $key);
 fclose($myfile);
user254153
  • 1,855
  • 4
  • 41
  • 84
  • it returns ascii value of keypressed. You can check for which key was pressed. – user254153 Jan 18 '15 at 17:06
  • Hi sorry that I haven't wrote back I've just been busy, anyway I tried the code and it didn't work, I couldn't see which key that was pressed but I'm not sure if I've been doing it right or not. But what I noticed it didn't work, thanks anyway ! – Nikki Jan 26 '15 at 09:34