php keydown / keyup通过串口发送值

I am trying to figure how I can use keyboard to send values assigned to keys via serial port on server side. When key is pressed the exact value is send in loop until key is released, then send another value once.

Tried to adapt and use this code:

<script id="jsbin-javascript">
$(document).ready(function () {

    $('body').on('keydown keyup', function( e ) {

      var key = e.which;
      var io  = e.type=="keydown" ? 0 : 1; // "0"if keydown; "1" if keyup
      var keyAction = {                    // Object to store our stuff
//  keyCode : [(0)KEYDOWN, (1)KEYUP]
        37  : ['37' ,'90'],            // left key
        39  : ['39','80'],            // Right key  
        38  : ['38','70'],               // Up key
        40  : ['40','60']              // Down key
      };
      var propObj = {};                    // Object to store property + value for jQuery methods
      var keyArr = keyAction[key];

      if(typeof keyArr != 'undefined') {   // Test keyArr (37,38,39,40) is returned/populated to prevent errors
         propObj.background = keyAction[key][io];   // need alter here
         $(this).css(propObj);                      // -"- 
      }

    });

});
</script>

Sending values from server via serial port to device works fine with simple code.

<?php 
exec("mode com1: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");
$fp =fopen("com1", "w");
fwrite($fp, $value);
fclose($fp);
?>

I am going mad when trying different ways without success. Thank you very much for help.