determine which key is pressed

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

How to determine which key is pressed in javascript?

4条回答
  •  闹比i
    闹比i (楼主)
    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:

    
    

提交回复
热议问题