陈建华的博客
专注web开发
js模拟键盘事件
2014-06-09 09:55:00   阅读1239次


我写了个例子,在IE6和Firefox3.5中已经测试过. 你要是想支持其他浏览器需要查相应的文档。 

用法:
创建一个新HTML文件,copy下面的内容进去就可以了

--------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Simulate Event</title>
<script type="text/javascript">
   function simulateKeyEvent(target, 
                                             keyCode)                            
    {
  
        if (!target){
            alert("Target is not exist");
        }
        
var customEvent = null;
var a = typeof document.createEvent;
        if(typeof document.createEvent == "function")
{
            try {
                
   //firefox
                customEvent = document.createEvent("KeyEvents");
                customEvent.initKeyEvent("keypress", true, true,window, false,
                    false,false, false, keyCode, keyCode);       
                
            } catch (ex){
                document.write("Shit happends. This example is only demonstrating event simulation in firefox and IE.");          
            }
            target.dispatchEvent(customEvent);
        } else if (document.createEventObject){ //IE
        
            customEvent = document.createEventObject();
            
            customEvent.bubbles = true;
            customEvent.cancelable = true;
            customEvent.view = window;
            customEvent.ctrlKey = false;
            customEvent.altKey = false;
            customEvent.shiftKey = false;
            customEvent.metaKey = false;
            customEvent.keyCode = keyCode;
            
            target.fireEvent("onkeypress", customEvent);  
                    
        } else {
document.write("This example is only demonstrating event simulation in firefox and IE.");
        }
    }
</script>
</head>
<body>
<input id="testinput" type="button" value="kick you"/>
</body>
<script type="text/javascript">
var target =document.getElementById("testinput");
   target.onkeypress = function(event){
          var event = (event)?event:window.event;
            if(event.keyCode == 27){
                alert('"ESC" pressed');
            }
    };
simulateKeyEvent(target,27);
</script>
</html>




-----------------------------------------------------
转载请注明来源此处
原地址:#

-----网友评论----
暂无评论
-----发表评论----
微网聚博客乐园 ©2014 blog.mn886.net 鲁ICP备14012923号   网站导航