Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7040857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:58:05+00:00 2026-05-28T01:58:05+00:00

I have a Script in my HTML document that detects when one of the

  • 0

I have a Script in my HTML document that detects when one of the arrow keys is detected then it changes the URL of an iframe. When the key is released the iframe is changed again.

This works except during the time that the key is being held down it keeps repeating the action to set the iframe’s url.

Is there a way so when its held down it only changes the iframe url once? Then when its released it changes the url back?

Extra:
Iv created a page to control a remote control car over the web. It works great except for this problem. Basicly it changes the iframe url to a PHP page with a diffrent get variable for each arrow key. The PHP page then uses PHP Serial to send data to the serial port based on what ever variable was sent from the iframe (using GET).

Thanks for any help!!!

Edit: Here’s a picture of the car. http://oi41.tinypic.com/21cimms.jpg

Edit 2

Heres the index.html

<!doctype html>
<html lang="en">
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script type="text/javascript">
        function pause(ms) {
           ms += new Date().getTime();
           while (new Date() < ms){}
          } 
            var keyBeingHeld = new Array();
            keyBeingHeld[0] = false;//left
            keyBeingHeld[1] = false;//right
            keyBeingHeld[2] = false;//up
            keyBeingHeld[3] = false;//down

            function setControllerState(action){
                leftPressState="up";
                rightPressState="up";
                upPressState="up";
                downPressState="up";
                if (keyBeingHeld[0]){
                    leftPressState="down";

                }
                if (keyBeingHeld[1]){
                    rightPressState="down";
                }
                if (keyBeingHeld[2]){
                    upPressState="down";
                }
                if (keyBeingHeld[3]){
                    downPressState="down";
                }
                var stateStr = "?upPressState="+upPressState+"&downPressState="+downPressState+"&leftPressState="+leftPressState+"&rightPressState="+rightPressState;
                iframe = document.getElementById('stateFrame');
                iframe.src = "BackEnd.php"+stateStr+"&action="+action;

            }

            $(document).keydown(function(event) {
                if (!event) var event = window.event;
                switch (event.keyCode) {
                    case 37: 
                        if (!keyBeingHeld[0]){
                            keyBeingHeld[0] = true;
                            setControllerState ("leftArrowPress");
                        } 
                        break;
                    case 38: 
                        if (!keyBeingHeld[2]){
                            keyBeingHeld[2] = true;
                            setControllerState ("upArrowPress");
                        } 
                        break;
                    case 39: 
                        if (!keyBeingHeld[1]){
                            keyBeingHeld[1] = true;
                            setControllerState ("rightArrowPress");
                        } 
                        break;
                    case 40: 
                        if (!keyBeingHeld[3]){
                            keyBeingHeld[3] = true;
                            setControllerState ("downArrowPress");

                        } 
                        break;
                }
            });

            $(document).keyup(function(event) {
                if (!event) var event = window.event;
                switch (event.keyCode) {
                    case 37: 
                        if (keyBeingHeld[0]){
                            keyBeingHeld[0] = false;
                            setControllerState ("leftArrowRelease");    
                        } 
                        break;
                    case 38: 
                        if (keyBeingHeld[2]){
                            keyBeingHeld[2] = false;
                            setControllerState ("upArrowRelease");

                        } 
                        break;
                    case 39: 
                        if (keyBeingHeld[1]){
                            keyBeingHeld[1] = false;
                            setControllerState ("rightArrowRelease");

                        } 
                        break;
                    case 40: 
                        if (keyBeingHeld[3]){
                            keyBeingHeld[3] = false;
                            setControllerState ("downArrowRelease");
                        } 
                        break;
                }
            });
        </script>
    </head>
    <body>
       <p>Press one of the arrow keys.</p>
       <iframe id="stateFrame" name="stateFrame" style="width:160px;height:180px;" src="BackEnd.php?upPressState=up&downPressState=up&leftPressState=up&downPressState=up&action=none"></iframe>
       <p>Thanks vdbuilder!</p>
    </body>
</html>

Heres the backend.php

<?php
    $opened = false;

    $upPressState = cleanupSpecialChars($_GET['upPressState']) or $upPressState = 'up';   
    $downPressState = cleanupSpecialChars($_GET['downPressState']) or $downPressState = 'up';
    $leftPressState = cleanupSpecialChars($_GET['leftPressState']) or $leftPressState = 'up';   
    $rightPressState = cleanupSpecialChars($_GET['rightPressState']) or $rightPressState = 'up';
    $action = cleanupSpecialChars($_GET['action']) or $action = 'none';

    // build your message from states and action, and send to serial port here 

    //the rest is to display the state
    $pressedColor = "bbffbb";
    $releasedColor = "bbbbbb";

    $upArrowColor = $releasedColor;
    $downArrowColor = $releasedColor;
    $leftArrowColor = $releasedColor;
    $rightArrowColor = $releasedColor;





    if($upPressState == "down"){ 
        sendCMD(25);
        $upArrowColor = $pressedColor;
    }
    if($downPressState == "down"){
        sendCMD(24);
        $downArrowColor = $pressedColor;
    }
    if($leftPressState == "down"){
        sendCMD(28);
        $leftArrowColor = $pressedColor;
    }
    if($rightPressState == "down"){
        sendCMD(29);
        $rightArrowColor = $pressedColor;
    }
    //--- Up
     if($upPressState == "up"){ 
        sendCMD(15);
        $upArrowColor = $releasedColor; 
    }
         if($downPressState == "up"){ 
        sendCMD(14);
        $downArrowColor = $releasedColor;   
    }
         if($leftPressState == "up"){ 
         sendCMD(18);
        $leftArrowColor = $releasedColor;   
    }
         if($rightPressState == "up"){ 
         sendCMD(19);
        $rightArrowColor = $releasedColor;  
    }

    echo("<html><head><style>");
    echo("body{background-color:#000000;}
          div#container{position:absolute;left:10px;top:10px;background-color:#eeeeee;
              font-size:20px;padding:20px;width:100px;height:92px;text-align:center;}
          div.arrow{position:absolute;width:30px;padding-top:2px;padding-bottom:2px;}
          div#upArrow{left:55px;top:20px;background-color:#".$upArrowColor.";}
          div#downArrow{left:55px;top:82px;background-color:#".$downArrowColor.";}
          div#leftArrow{left:20px;top:50px;background-color:#".$leftArrowColor.";}
          div#rightArrow{left:90px;top:50px;background-color:#".$rightArrowColor.";}
          div#lastAction{position:absolute;left:0px;bottom:2px;font-size:14px;color:#ffffee;}");
    echo("</style></head><body>");
    echo("<div id='container'>
          <div class='arrow' id='upArrow'>&uarr;</div><div class='arrow' id='downArrow'>&darr;</div>
          <div class='arrow' id='leftArrow'>&larr;</div><div class='arrow' id='rightArrow'>&rarr;</div>
          </div><div id='lastAction'>Last Action:<br />".$action."</div>");
    echo("</body></html>");

//cleanup input
    function cleanupSpecialChars($inStr){
        $tempStr = htmlentities(stripslashes($inStr));
        $retStr = str_replace(array('\\','/'), '', $tempStr);
        return $retStr;
    }
    function sendCMD($cmd){
        if($opened == false){
        $fp =fopen("com4", "wb");
         } 
        fwrite($fp,$cmd);
        fclose($fp);
        }


?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:58:06+00:00Added an answer on May 28, 2026 at 1:58 am

    Short answer:

    Set a flag and test if it’s set before you send the request.

    Long answer:

    Two files are required.
    The two files below work as follows:

    You navigate to FrontEnd.html. It maintains state and contains an iframe. The iframe is loaded with BackEnd.php (with its state variables set to default values and passed via the get method). When a key is pressed or released FrontEnd.html reloads the iframe with BackEnd.php (passing state via get method).

    BackEnd.php sends the message to the serial port and displays the state of the arrow keys, based on the variables passed to it from FrontEnd.html.

    <!doctype html>
    <html lang="en">
        <head>
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
            <script type="text/javascript">
                var controllerIsReady = false;
                var keyEventBuffer = new Array();
                var keyBeingHeld = new Array();
                keyBeingHeld[0] = false;//left
                keyBeingHeld[1] = false;//right
                keyBeingHeld[2] = false;//up
                keyBeingHeld[3] = false;//down
    
                function controllerReady(){
                    controllerIsReady = false;
                    if(keyEventBuffer.length > 0){
                        bufferDiv = document.getElementById('eventBuffer');
                        buffHtmlStr = " ";
                        for (i=0;i<keyEventBuffer.length;i++){
                            tempStr = keyEventBuffer[i];
                            index = tempStr.indexOf("action", 0)+7;
                            keyEventStr = tempStr.substr(index,(tempStr.length-index));
                            buffHtmlStr = buffHtmlStr + "<br />" + keyEventStr;
                        }
                        bufferDiv.innerHTML = buffHtmlStr;
                        //shift event off and make request
                        stateStr = keyEventBuffer.shift(); 
                        iframe = document.getElementById('stateFrame');
                        iframe.src = "http://www.vdstudios.net/robotics/BackEnd.php"+stateStr; 
                    }
                    else{
                        controllerIsReady = true;//alert("ready");
                        bufferDiv = document.getElementById('eventBuffer');
                        bufferDiv.innerHTML = "empty";
                    }
    
                }
    
                function setControllerState(action){
                    leftPressState="up";
                    rightPressState="up";
                    upPressState="up";
                    downPressState="up";
                    if (keyBeingHeld[0]){
                        leftPressState="down";
                    }
                    if (keyBeingHeld[1]){
                        rightPressState="down";
                    }
                    if (keyBeingHeld[2]){
                        upPressState="down";
                    }
                    if (keyBeingHeld[3]){
                        downPressState="down";
                    }
                    var stateStr = "?upPressState="+upPressState+"&downPressState="+downPressState+"&leftPressState="+leftPressState+"&rightPressState="+rightPressState+"&action="+action;
    
                    //add stateStr to fifo and if controllerIsReady call controllerReady
                    keyEventBuffer.push(stateStr);
                    if(controllerIsReady){
                        controllerReady();
                    }
                }
    
                $(document).keydown(function(event) {
                    if (!event) var event = window.event;
                    switch (event.keyCode) {
                        case 37: 
                            if (!keyBeingHeld[0]){
                                keyBeingHeld[0] = true;
                                setControllerState ("leftArrowPress");
                            } 
                            break;
                        case 38: 
                            if (!keyBeingHeld[2]){
                                keyBeingHeld[2] = true;
                                setControllerState ("upArrowPress");
                            } 
                            break;
                        case 39: 
                            if (!keyBeingHeld[1]){
                                keyBeingHeld[1] = true;
                                setControllerState ("rightArrowPress");
                            } 
                            break;
                        case 40: 
                            if (!keyBeingHeld[3]){
                                keyBeingHeld[3] = true;
                                setControllerState ("downArrowPress");
                            } 
                            break;
                    }
                });
    
                $(document).keyup(function(event) {
                    if (!event) var event = window.event;
                    switch (event.keyCode) {
                        case 37: 
                            if (keyBeingHeld[0]){
                                keyBeingHeld[0] = false;
                                setControllerState ("leftArrowRelease");
                            } 
                            break;
                        case 38: 
                            if (keyBeingHeld[2]){
                                keyBeingHeld[2] = false;
                                setControllerState ("upArrowRelease");
                            } 
                            break;
                        case 39: 
                            if (keyBeingHeld[1]){
                                keyBeingHeld[1] = false;
                                setControllerState ("rightArrowRelease");
                            } 
                            break;
                        case 40: 
                            if (keyBeingHeld[3]){
                                keyBeingHeld[3] = false;
                                setControllerState ("downArrowRelease");
                            } 
                            break;
                    }
                });
            </script>
        </head>
        <body>
           <p>Press one of the arrow keys.</p>
           <iframe id="stateFrame" name="stateFrame" style="width:160px;height:180px;" src="http://www.vdstudios.net/robotics/BackEnd.php?upPressState=up&downPressState=up&leftPressState=up&rightPressState=up&action=none"></iframe>
           <div style="width:160px;padding:2px;padding-top:5px;background-color:#cccccc;text-align:center;">Event buffer<hr /><div id="eventBuffer" style="width:100%;">text</div></div> 
        </body>
    </html>
    

    BackEnd.php

    <?php
        header("Cache-Control: no-store, no-cache, must-revalidate"); 
        header("Cache-Control: post-check=0, pre-check=0", false); 
        // HTTP/1.0 
        header("Pragma: no-cache");
    
        $upPressState = cleanupSpecialChars($_GET['upPressState']) or $upPressState = 'up';   
        $downPressState = cleanupSpecialChars($_GET['downPressState']) or $downPressState = 'up';
        $leftPressState = cleanupSpecialChars($_GET['leftPressState']) or $leftPressState = 'up';   
        $rightPressState = cleanupSpecialChars($_GET['rightPressState']) or $rightPressState = 'up';
        $action = cleanupSpecialChars($_GET['action']) or $action = 'none';
    
        //send message to serial port 
        if($action == "upArrowPress"){
            sendCMD(25);
        }
        else if($action == "downArrowPress"){
            sendCMD(24);
        }
        else if($action == "leftArrowPress"){
            sendCMD(28);
        }
        else if($action == "rightArrowPress"){
            sendCMD(29);
        }
        else if($action == "upArrowRelease"){
            sendCMD(15);
        }
        else if($action == "downArrowRelease"){
            sendCMD(14);
        }
        else if($action == "leftArrowRelease"){
            sendCMD(18);
        }
        else if($action == "rightArrowRelease"){
            sendCMD(19);
        }
    
        //the rest is to display the state
        $pressedColor = "bbffbb";
        $releasedColor = "bbbbbb";
    
        $upArrowColor = $releasedColor;
        $downArrowColor = $releasedColor;
        $leftArrowColor = $releasedColor;
        $rightArrowColor = $releasedColor;
    
        if($upPressState == "down"){
            $upArrowColor = $pressedColor;
        }
        if($downPressState == "down"){
            $downArrowColor = $pressedColor;
        }
        if($leftPressState == "down"){
            $leftArrowColor = $pressedColor;
        }
        if($rightPressState == "down"){
            $rightArrowColor = $pressedColor;
        }
    
        echo("<html><head><style>");
        echo("body{background-color:#000000;}
              div#container{position:absolute;left:10px;top:10px;background-color:#eeeeee;
                  font-size:20px;padding:20px;width:100px;height:92px;text-align:center;}
              div.arrow{position:absolute;width:30px;padding-top:2px;padding-bottom:2px;}
              div#upArrow{left:55px;top:20px;background-color:#".$upArrowColor.";}
              div#downArrow{left:55px;top:82px;background-color:#".$downArrowColor.";}
              div#leftArrow{left:20px;top:50px;background-color:#".$leftArrowColor.";}
              div#rightArrow{left:90px;top:50px;background-color:#".$rightArrowColor.";}
              div#lastAction{position:absolute;left:0px;bottom:2px;font-size:14px;color:#ffffee;}");
        echo("</style></head><body onload='parent.controllerReady();'>");
        echo("<div id='container'>
              <div class='arrow' id='upArrow'>&uarr;</div><div class='arrow' id='downArrow'>&darr;</div>
              <div class='arrow' id='leftArrow'>&larr;</div><div class='arrow' id='rightArrow'>&rarr;</div>
              </div><div id='lastAction'>Last Action:<br />".$action."</div>");
        echo("</body></html>");
    
    //cleanup input
        function cleanupSpecialChars($inStr){
            $tempStr = htmlentities(stripslashes($inStr));
            $retStr = str_replace(array('\\','/'), '', $tempStr);
            return $retStr;
        }
        function sendCMD($cmd){
            if($fp =fopen("com4", "wb")){//windows server
                fwrite($fp,$cmd);
                usleep(15000);
                fclose($fp);
            }
            else if($fp =fopen("/dev/ttyS3", "wb")){//linux server
                fwrite($fp,$cmd);
                usleep(15000);
                fclose($fp);
            }
        }
    ?>
    

    You can see them running on my server here: http://www.vdstudios.net/robotics/FrontEnd.html

    I can help you with building the message for the serial port if you give me details on what the device expects.

    EDIT:

    Added the code to build/send the message to the serial port. Also updated the running example with the same. You can see it at the link above.

    Edit 2:

    Added the code for buffering the keyEvents.
    Added the code for sync’ing the backend and frontend states.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script that uses Selenium to fetch a HTML document called
I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The
I have an upload script that write a index.html file, I modified it to
I have a livesearch script that i need to populate my html menu with
Within my HTML, I have a php script that includes a file. At that
While declaring Javascript in a html document. We have 3 ways to do that
I have a jqGrid script like that: jQuery(document).ready(function() { var startDate = $(#startDate).Val(); jQuery(#sandgrid).jqGrid({
I have created PS script that queries the Event Logs on my Servers then
I have script that loops through an array of html tag id's, some elements
I have a C# application that generates an html document from transforming an xml

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.