分享一篇获取键盘数值的js(限制在IE浏览器)

<html>
<!--//this code by hongseheike-->
<head>
<script language="javascript">
    ns4 = (document.layers) ? true : false;
    ie4 = (document.all) ? true : false;
function keyDown(e){
    if(ns4){
    var nkey=e.which;
    var iekey='现在是ns浏览器';
    var realkey=String.fromCharCode(e.which);
}
    if(ie4){
    var iekey=event.keyCode;
    var nkey='现在是ie浏览器';
    var realkey=String.fromCharCode(event.keyCode);
    if(event.keyCode==32){realkey='\' 空格\''}
    if(event.keyCode==13){realkey='\' 回车\''}
    if(event.keyCode==27){realkey='\' Esc\''}
    if(event.keyCode==16){realkey='\' Shift\''}
    if(event.keyCode==17){realkey='\' Ctrl\''}
    if(event.keyCode==18){realkey='\' Alt\''}
}
    alert('ns浏览器中键值:'+nkey+'\n'+'ie浏览器中键值:'+iekey+'\n'+'实际键为'+realkey);
}
document.onkeydown = keyDown;
if(ns4){
document.captureEvents(Event.KEYDOWN);}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>
<body>
//Javascript Document
<hr>
<center>
<h3>请按任意一个键。。。。</h3>
</center>
</body>
</html> 

原文:http://hi.baidu.com/collect_zero/item/966b3ccf43958e3f44941637

posted @ 2012-11-26 22:04  沉睡的泰坦尼克  阅读(178)  评论(1编辑  收藏  举报