【JS】键盘事件
1、所有的键监听事件(keydown、keyup)
var textbox = document.getElementById("myText"); EventUtil.addHandler(textbox, "keydown", function(event){ event = EventUtil.getEvent(event); alert(event.keyCode); }); EventUtil.addHandler(textbox, "keyup", function(event){ event = EventUtil.getEvent(event); alert(event.keyCode); });
2、字符编码的键盘按下监听事件(影响input框的文本的键)(keypress)
<input type="text" id="myText" /> <script type="text/javascript"> var textbox = document.getElementById("myText"); EventUtil.addHandler(textbox, "keypress", function(event){ var code = EventUtil.getCharCode(event);
alert(code);
alert("对应键盘上的:"+String.fromCharCode(code)); }); </script>
3、可编辑区域发生变化时的字符编码的键盘监听事件(textInput)
支持的浏览器有:IE9+、Safari和Chrome
<input type="text" id="myText" /> <p>This example only works in browsers that support the <code>textInput</code> event.</p> <script type="text/javascript"> var textbox = document.getElementById("myText"); EventUtil.addHandler(textbox, "textInput", function(event){ event = EventUtil.getEvent(event); alert(event.data);//键盘上的实际值 }); </script>