Possible Duplicate:
js: how to find out what character key is pressed?

I want to know how to make an event on key pressing and to specify which key is pressed using Javascript only.

有帮助吗?

解决方案

<script type="text/javascript">
    function myKeyPress(e){

        var keynum;

        if(window.event){ // IE                 
            keynum = e.keyCode;
        }else
            if(e.which){ // Netscape/Firefox/Opera                  
                keynum = e.which;
             }
        alert(String.fromCharCode(keynum));
    }
   </script>


 <form>
  <input type="text" onkeypress="return myKeyPress(event)" />
 </form>

Referred from How to find out what character key is pressed?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top