@hunter129

天天学习,好好向上!

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  21 随笔 :: 5 文章 :: 37 评论 :: 0 Trackbacks
做web开发的时候,有时候需要根据键盘进行一些操作,例如按下Enter的时候提交表单,禁止用户输入某些特殊字符,设置快捷键等等。这时候需要找出用户按下的是那些按键,每次都找对照表太麻烦了.so..写了这么个小程序来测试按键。^_^
其中的charCode是根据ascii表转换的,不一定准确。

keyCode :
charCode :
shift-key :
ctrl-key :
alt-key :

下面是ascii编码表:
ascii编码表
源代码:
<script type="text/javascript">
function showKey(e){
= e || window.event;
document.getElementById(
"keyCode").value = e.keyCode;
document.getElementById(
"charCode").value = String.fromCharCode(e.keyCode);
document.getElementById(
"shiftKey").value = e.shiftKey;
document.getElementById(
"ctrlKey").value = e.ctrlKey;
document.getElementById(
"altKey").value = e.altKey;
}
$(document).ready(
function(){
document.onkeydown 
= showKey;
});
</script>
<br />
<table>
    
<tbody>
        
<tr>
            
<td>keyCode : </td>
            
<td><input id="keyCode" type="text" /> </td>
        
</tr>
        
<tr>
            
<td>charCode  : </td>
            
<td><input id="charCode" type="text" /> </td>
        
</tr>
        
<tr>
            
<td>shift-key : </td>
            
<td><input id="shiftKey" type="text" /> </td>
        
</tr>
        
<tr>
            
<td>ctrl-key : </td>
            
<td><input id="ctrlKey" type="text" /> </td>
        
</tr>
        
<tr>
            
<td>alt-key : </td>
            
<td><input id="altKey" type="text" /> </td>
        
</tr>
    
</tbody>
</table>
其中我使用了jquery来初始化document的onkeydown事件。如果你不使用jquery可以用下面这段代码
window.onload = function(){
  document.onkeydown 
= showKey;
};

代替
$(document).ready(function(){
document.onkeydown 
= showKey;
});

posted on 2008-02-02 17:00 hunter129 阅读(1591) 评论(5)  编辑  收藏 所属分类: js

评论

# re: 测试你按下的是那个按键(附 ascii编码表) 2008-02-29 11:39 CoderDream
请把源代码贴出来!  回复  更多评论
  

# re: 测试你按下的是那个按键(附 ascii编码表) 2008-03-04 13:37 落Nicety
@CoderDream
这位仁兄,其实你可以在页面源代码里面看到^_^
不过为了方便 还是贴出来吧~~  回复  更多评论
  

# re: 测试你按下的是那个按键(附 ascii编码表) 2008-03-27 11:07 zzb
好东西!!!  回复  更多评论
  

# re: 测试你按下的是那个按键(附 ascii编码表) 2008-04-04 14:38 落Nicety
@zzb
thanks~  回复  更多评论
  

# re: 测试你按下的是那个按键(附 ascii编码表) 2009-01-05 18:17 fdfd
范德萨范德萨  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: