2012-08-09 804 views
0

如何通过javascript在网页上模拟ESC按键?如何通过javascript模拟网页上的ESC按键?

非常感谢您的帮助!

+0

可能重复[模拟左,用JavaScript右箭头键事件](http://stackoverflow.com/questions/ 5516600/simulate-left-and-right-arrow-key-event-with-javascript) – 2012-08-09 12:10:11

+0

这个答案是[使用initKeyboardEvent](http://stackoverflow.com/questions/11857524/working-with-initkeyboardevent/11857825# 11857825)可能会有所帮助。 – RobG 2012-08-09 12:20:35

回答

-1

没有jQuery的:

document.onkeydown = checkKeycode; 
function checkKeycode(e) { 
var keycode; 
    if (window.event) { 
    keycode = window.event.keyCode; 
    } 
    else if (e) { 
    keycode = e.which; 
    } 
    // enter 
    if (keycode == 13) { 

    } 

    // escape 
    if (keycode == 27) { 
    //place your action here 
    } 
} 
+0

但我需要通过javascript代码(不是由用户)按ESC。 – Dmitry 2012-08-09 12:08:19

+0

这是检测何时按下Esc键,它不会模拟它。 – 2012-08-09 12:10:02

-1

你的意思是这样

$(document).delegate('input','keypress', function(e){ 
    code = e.keyCode ? e.keyCode : e.which; 
    if(code=== 27){ 
     alert('esc pressed'); 
    } 
}); 
+0

这是检测何时按下esc键,它不会模拟它。 – 2012-08-09 12:08:19