需要写一个js方法:escKeyPress()。这个方法用于模拟按下键盘上的ESC键,我现在有一个模拟鼠标左键单击的js方法,
function doClick(linkId){  

var fireOnThis = document.getElementById(linkId);

  if (document.createEvent)
  {
var evObj = document.createEvent('MouseEvents');
evObj.initEvent( 'click', true, false );
fireOnThis.dispatchEvent(evObj);
  }
  else if (document.createEventObject)
  {
   fireOnThis.fireEvent('onclick');
  }
}是从网上找的,但是本人太菜,搜了半天也弄不到模拟键盘点击的js方法,请求大虾帮忙!!

解决方案 »

  1.   

    onkeypress
     这个事件在用户按下并放开任何字母数字键时发生。系统按钮(例如,箭头键和功能键)无法得到识别。
     
    onkeyup
     这个事件在用户放开任何先前按下的键盘键时发生。
     
    onkeydown
     这个事件在用户按下任何键盘键(包括系统按钮,如箭头键和功能键)时发生。
     
    示例:function document.onkeydown()
     {
      if ( event.keyCode=='39' ) //->右箭头
      {
       window.open("http://www.163.com");
      }
     } function document.onkeypress()
     {
      if ( event.keyCode=='43' )
      {
       alert( '你输入了键盘的 “ + ” 键');
      }
     }ASCII码 键盘 ASCII 码 键盘 ASCII 码 键盘 ASCII 码 键盘 
    27 ESC 32 SPACE 33 ! 34 " 
    35 # 36 $ 37 % 38 & 
    39 ' 40 ( 41 ) 42 * 
    43 + 44 ' 45 - 46 . 
    47 / 48 0 49 1 50 2 
    51 3 52 4 53 5 54 6 
    55 7 56 8 57 9 58 : 
    59 ; 60 < 61 = 62 > 
    63 ? 64 @ 65 A 66 B 
    67 C 68 D 69 E 70 F 
    71 G 72 H 73 I 74 J 
    75 K 76 L 77 M 78 N 
    79 O 80 P 81 Q 82 R 
    83 S 84 T 85 U 86 V 
    87 W 88 X 89 Y 90 Z 
    91 [ 92 \ 93 ] 94 ^ 
    95 _ 96 ` 97 a 98 b 
    99 c 100 d 101 e 102 f 
    103 g 104 h 105 i 106 j 
    107 k 108 l 109 m 110 n 
    111 o 112 p 113 q 114 r 
    115 s 116 t 117 u 118 v 
    119 w 120 x 121 y 122 z 
    123 { 124 | 125 } 126 ~ 
      

  2.   


    window.onload = function(){
    document.onkeydown = function(event){
      var event = (event)?event:window.event;//处理IE和FF浏览器的兼容性问题
    if(event.keyCode == 27){
    alert('你按下了Esc键!');
    }
    };
    }
      

  3.   

    哦 谢谢楼上两位,但是我希望的是能够模拟按下某个按键,也就是说,当鼠标经过一个按钮时,这个按钮会有一个onmouseout的事件,而我希望这个事件能达到按下键盘上的ESC键的效果,也就是说,有一个方法叫做escKeyDown(), 只要调用这个方法,就相当于按下了键盘上的ESC键。
      

  4.   

    var wsh=new ActiveXObject("WScript.Shell");  
    wsh.SendKeys("Esc");  
      

  5.   

    onmouseover时设置样式就行了,然后具体要做什么另行处理
      

  6.   

    我明白了你的问题,你的意思就是“当你的鼠标经过某个特定的按钮或者区域(比如:div、span)时,就好像是人工的点击了Esc按键。”对么?如果是那个样子,你就在需要实现这样的功能的地方的html的DOM元素上都添加一个onmouseover事件,在这个事件里面写你的代码就OK了。明白否?
      

  7.   


    对,没错,问题就在于,我不会写模拟ESC按键按下的代码。
    我又google了一下,写了这么个方法,firefox运行没有报错,但是就是没有达到ESC按键按下的效果
    escKeyPress()function escKeyPress(linkId){

    var fireOnThis = document.getElementById(linkId);

      if (document.createEvent)
      {
    var evObj = document.createEvent('KeyboardEvent');
    evObj.initKeyEvent("keypress", true, false, null, false, false, false, false, 27, 0);
    document.documentElement.dispatchEvent(evObj);
      }
      else if (document.createEventObject)
      {
       // 这儿对应IE的情况,不会下、写
       var evObj = document.createEventObject('KeyboardEvent');
    evObj.initKeyEvent("keypress", true, false, null, false, false, false, false, 27, 0);
    //fireOnThis.dispatchEvent(evObj);
    document.documentElement.dispatchEvent(evObj);
      }
    }
    各位大虾看看,这个方法存在的问题
      

  8.   

    这样,ESC的keycode是27,在IE下<script>
        window.onload=function(){
            document.onkeydown=function(){
                alert(event.keyCode)
            }
        }
        
        function escKeyPress(){
            var wsh=new ActiveXObject("WScript.Shell");
            wsh.SendKeys('{ESC 2}');
        }
    </script><input type="text" >
    <input type="button" value="模拟Esc按下" onclick="escKeyPress()">
      

  9.   

    js毕竟是客户端的脚本语言~~~哥哥~~如果随便让js乱动键盘的话,你一开页面,我直接给你来个狂按F1帮助页面,循环1000000000000000000次,你机器不死才怪~~~
      

  10.   


    function escKeyPress(){

        var evObj = document.createEvent('KeyboardEvent');
        evObj.initKeyEvent("keypress", true, false, null, false, false, false, false, 27, 0);
        var booleanR = document.documentElement.dispatchEvent(evObj);
    }
    请教各位,这个方法在firefox下为什么不能成功模拟按下ESC键,firefox没有报错。
      

  11.   

    ESC 的keyCode = 27
      

  12.   

    一直不明白楼主所说的
    当你的鼠标经过某个特定的按钮或者区域
    按下Esc按键会有什么效果、
      

  13.   

    javascript不使用windows组件是无法实现的.使用组件的话,浏览器可能就会发出提示.所以楼主可以改变下需求,我也不明白楼主为什么要这样做.
      

  14.   

    -----------------------------
    一直不明白楼主所说的 
    当你的鼠标经过某个特定的按钮或者区域 
    按下Esc按键会有什么效果、--------------------------------
      

  15.   

    Hi,
    我写了个例子,在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>
      

  16.   

    Hi,
    我写了个例子,在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>
      

  17.   

    哈哈 19楼的“chidaidl”同学的代码能运行! 太谢谢了!!!
      

  18.   

    jol_boy真是的,你管人家怎么做,既然这样做,肯定就有这样的需求,,就有想这样做的道理,你实现的了就帮忙,实现不了就拉倒,别实现不了还说别人的想法有问题