博客年龄:16年6个月
访问:?
文章:21篇

按键事件

分类:JS | 标签: 键盘事件   js  
2010-07-13 10:15 阅读(?)评论(0)

<script type="text/javascript">
//By 枫岩@CnLei.Com
function $(s){
  return document.getElementById(s)?document.getElementById(s):s;
}
function viewKeyInfo(e){
  var currKey=0,CapsLock=0;
  var e=e||event;
  currKey=e.keyCode||e.which||e.charCode;
  CapsLock=currKey >=65 && currKey <=90;
  $("type").innerHTML=e['type'];
  $("currKey").innerHTML=String.fromCharCode(currKey);
  $("Decimal").innerHTML=currKey;
  $("keyCode").innerHTML=e['keyCode'];
  $("charCode").innerHTML=e['charCode'];
  $("caps").innerHTML=CapsLock;
  $("shiftKey").innerHTML=e['shiftKey'];
  $("ctrlKey").innerHTML=e['ctrlKey'];
  $("repeat").innerHTML=e['repeat'];
  $("which").innerHTML=e['which'];
alert(currKey);
}
//document.onkeypress= viewKeyInfo;
document.onkeydown= viewKeyInfo;
//window.event = viewKeyInfo;
alert(currKey);
//  alert(e['keyCode']);
</script>
<p>请按下任意键看测试效果:</p>
type:<span id="type"></span><br />
当前Key:<span id="currKey"></span><br />
Decimal:<span id="Decimal"></span><br />
keyCode:<span id="keyCode"></span> <strong>注:在FF下,keyCode始终为0</strong><br />
which:<span id="which"></span> <strong>注:在IE下,which始终为undefined ; 在Opera下,keyCode和charCode二者的值相同</strong><br />
charCode:<span id="charCode"></span> <strong>注:在IE、Opera下,charCode始终为undefined ; 在FF下,which和charCode二者的值相同</strong><br />
大写:<span id="caps"></span><br />
altKey:<span id="altKey"></span><br />
ctrlKey:<span id="ctrlKey"></span><br />
shiftKey:<span id="shiftKey"></span><br />
repeat:<span id="repeat"></span><br />
<style type="text/css" media="all">
body {color:#999;font:normal 14px tahoma,宋体,Geneva,Arial,sans-serif;}
span {color:#f00;font-weight:bold;padding:0 5px;}
strong {color:#090;font-weight:normal;padding:0 5px;}
</style>
 

 

   阅读(?)评论(0)
上一篇: js获取参数 下一篇:该日志被锁定
 
表  情:
加载中...
 

请各位遵纪守法并注意语言文明