Ticker

6/recent/ticker-posts

JavaScript Keystroke Action

Good afternoon visitors, sorry if that reading this article at night, or morning. This time I will share a little story yah ya could say that because this is not a tutorial

The beginning of the story of my client asked for a program to control a robot by using the up, down, left, right there at the keyboard, because the robot that will be created using the ip camera and to more easily access the IP camera was then used web browser. It occurred to me how the minds controler and ip camera can be accessed by diletkkan in one program with web-based programming languages.
Then I select the web programming language uses javascript. Why javascript? nyesuain the hell wrote needs, so when I want to control the robot forward or backward without having to move to another program web pages with different commands every page as PHP. And the good news has javascript keystroke capture function (search on google about keystroke) from the keyboard in real time, and this is what I need.

Already forget it just my ramblings above it only curhatan me. Focus wrote to the title we will not create a robot here. I take a quote from the source program
W3c and I will give an example of a small program that will condition an action of the program to key on the keyboard is pressed. But before the first understand the code that will be issued by the javascript program when the keyboard key is pressed, it is tantamount to perform interfacing using the keyboard, the following code will be issued by the program can be accessed here 

or you can try the following program to find out the code of each keyboard button, save the program under the extension ".html"
 
<!DOCTYPE html>
<html>
<body>

<p>Press a key on the keyboard in the input field to get the Unicode character code of the pressed key.</p>

<input type="text" size="40" onkeypress="myFunction(event)">

<p id="demo"></p>

<script>
/* In this example, we use a cross-browser solution, because the keyCode property does not work on the onkeypress event in Firefox. However, the which property does.

Explanation of the first line in the function below: if the browser supports event.which, then use event.which, otherwise use event.keyCode */

function myFunction(event) {
    var x = event.which || event.keyCode;
    document.getElementById("demo").innerHTML = "The Unicode value is: " + x;
}
</script>

</body>
</html>
 
To obtain the desired keycode then point the cursor in the text box and press the button to search key code themselves on the keyboard and I get a code like the following:
left button     = 37
up button      = 38
right button  = 39
down button = 40

For his evidence I love clay ss deh one wrote it I press the left exit code.

  
  
Now we create a program that will give the action of the condition key is pressed on the keyboard. Kayaking program below yes

<!DOCTYPE html>
<html>
<body>

<p>Press the Escape/Esc key on the keyboard (usually in the top left corner) in the input field, to alert some text.</p>

<input type="text" size="50" onkeydown="keyCode(event)">

<script>
function keyCode(event) {
    var x = event.keyCode;
    if (x == 37) {
        alert ("Left!");
    }

    if (x == 38) {
        alert ("Up!");
    }

    if (x == 39) {
        alert ("Right!");
    }
    if (x == 40) {
        alert ("Down!");
    }
}
  </script>

</body>
</html>


Save with the extension ".html" and trial by putting the cursor on a textbox and pressing the left, right, up, down see the result would be no alerts as shown below when I pressed the button down.


Note:

Actions of any state when keystrokes can be changed as calling web page or write to the file (echo) as I did to control the robot.


Okay so hopefully help
  

Post a Comment

0 Comments