determine which key is pressed

后端 未结 4 765
南方客
南方客 2021-01-29 14:42

How to determine which key is pressed in javascript?

相关标签:
4条回答
  • 2021-01-29 14:53

    In jQuery:

    jQuery(window).live('keydown', function(e) { 
      var keyCode = e.keyCode || e.which; 
    
      //do stuff with keycode
      } 
    });
    

    Normal:

    document.onkeyup = KeyCheck;       
    
    function KeyCheck(){
    
       var KeyID = event.keyCode;
       //do stuff
    }
    
    0 讨论(0)
  • 2021-01-29 14:57

    in the function which takes the key event:

    function(e){
       var key = String.fromCharCode(e.keyCode);
    }
    
    0 讨论(0)
  • 2021-01-29 14:57

    In 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));
        alert(keynum);
        if (keynum == 13) {
            alert("You Pressed Enter Key");
        }
    }
    

    In HTML:

    <textarea name="box" id="box" onkeypress="return myKeyPress(event)"></textarea>
    
    0 讨论(0)
  • 2021-01-29 15:03

    You can handle the keydown and keyup events on the document and set a flag for each key.

    0 讨论(0)
提交回复
热议问题