www.webdeveloper.com
Results 1 to 4 of 4

Thread: How to run function when a key is pressed?

  1. #1
    Join Date
    Apr 2013
    Posts
    2

    How to run function when a key is pressed?

    So basically what i want to do is when you press a specific key a function you assign it to will run. I dont know if you use addEventListener or what. Thank you!

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    See, for example, this page from w3schools.com

  3. #3
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,667
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script> vic=0; </script>
    <form name=Show id=Show style="position:absolute;visibility:visible;top:700px;left:0px;" >
    <input size=100 name=Show0 >
    <input size=10 name=Show1 >
    <input size=10 name=Show2 >
    <input size=10 name=Show3 >
    <input size=10 name=Show4 >
    <input size=10 name=Show5 >
    <input size=10 name=Show6 >
    <input size=10 name=Show7 >
    <input size=10 name=Show8 >
    <input size=10 name=Show9 ><br>
    <textarea name=TA rows=1 cols=100 ></textarea>
    </form>
    <script type="text/javascript">
    /*<![CDATA[*/
    function KeyCode(e){
     e=e||window.event;
     var charCode=e.which?e.which:event.keyCode ;
    document.Show.Show0.value=charCode;
    document.Show.Show1.value=String.fromCharCode(charCode);
     return false;
    }
    document.onkeypress=function(evt){ KeyCode(evt); }
    document.onkeydown=function(evt){  KeyCode(evt); }
    
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    @vwphillips

    Why a variable vic ? Why ten inputs to use only two ? Why inputs at 700 px of the top ?
    Is it forbidden to detect key moves without input ?

    We are Sunday...

    A minimal variant
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
      <title></title>
    </head>
    <body>
    <script type="text/javascript">
    document.onkeypress=function(e){e=e||window.event;alert(e.which?e.which:event.keyCode);}
    </script>
    </body>
    </html>
    Last edited by 007Julien; 04-28-2013 at 06:52 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles