js检测键盘事件、键对应的unicode码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>键盘事件</title>
<script type="text/javascript">
function Kpress()
{
 var ev=window.event;
 alert(ev.type);
 if(ev.altKey)
 {
  alert("按下了alt键");
 }
 else if(ev.shiftKey)
 {
  alert("按下了shift键");
 }
 else if(ev.ctrlKey)
 {
  alert("按下了ctrl键");
 }
 else
 {
  var code=ev.keyCode;
  alert(code);
 }
}
function Kdown()
{
 var ev=window.event;
 alert(ev.type);
 if(ev.altKey)
 {
  alert("按下了alt键");
 }
 else if(ev.shiftKey)
 {
  alert("按下了shift键");
 }
 else if(ev.ctrlKey)
 {
  alert("按下了ctrl键");
 }
 else
 {
  var code=ev.keyCode;
  alert(code);
 }
}
function Kup()
{
 var ev=window.event;
 alert(ev.type);
 if(ev.altKey)
 {
  alert("按下了alt键");
 }
 else if(ev.shiftKey)
 {
  alert("按下了shift键");
 }
 else if(ev.ctrlKey)
 {
  alert("按下了ctrl键");
 }
 else
 {
  var code=ev.keyCode;
  alert(code);
 }
}
</script>
</head>

<body>
<input type="text" value="" id="id1" onkeypress="Kpress()" onkeydown="Kdown()" onkeyup="Kup()" />

</body>
</html>

posted @ 2009-10-27 14:41  坐看风起  阅读(1774)  评论(2编辑  收藏  举报