KeyboardEvent in Chrome, keyCode is 0

后端 未结 6 1536
北海茫月
北海茫月 2020-12-05 13:32

I am trying to set keyCode on dispatched event object .

On button click, event is raised on textbox control to simulating keydown event. Event is raised successfully

6条回答
  •  离开以前
    2020-12-05 14:06

    This happens because (as others have stated) the KeyboardEvent object (which uses charCode instead of keyCode) is being called instead of the Event object. When you are using:

    function txtKeydown(e) {
        console.dir(e);
        console.log(e.keyCode);
    }
    

    if you're getting a 0 printed to the console, try using e.charCode instead of e.keyCode

    refer to MDN web docs for more information regarding KeyboardEvent: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent

提交回复
热议问题