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.

6
  • you want to write in text file which key was pressed
    – user254153
    Jan 18, 2015 at 16:12
  • You cannot write to files using javascripts. stackoverflow.com/questions/582268/…
    – user254153
    Jan 18, 2015 at 16:36
  • You can write with php.
    – user254153
    Jan 18, 2015 at 16:36
  • Ok is there any way for you to show me how ?
    – Nikki
    Jan 18, 2015 at 16:39
  • You can store to database or text files using php..
    – user254153
    Jan 18, 2015 at 16:46

2 Answers 2

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.

1
  • I tried this one to and it didn't make any differens from the original code, thanks anyway !
    – Nikki
    Jan 26, 2015 at 9: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);
2
  • it returns ascii value of keypressed. You can check for which key was pressed.
    – user254153
    Jan 18, 2015 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, 2015 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.