需要写一个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方法,请求大虾帮忙!!
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方法,请求大虾帮忙!!
这个事件在用户按下并放开任何字母数字键时发生。系统按钮(例如,箭头键和功能键)无法得到识别。
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 ~
window.onload = function(){
document.onkeydown = function(event){
var event = (event)?event:window.event;//处理IE和FF浏览器的兼容性问题
if(event.keyCode == 27){
alert('你按下了Esc键!');
}
};
}
wsh.SendKeys("Esc");
对,没错,问题就在于,我不会写模拟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);
}
}
各位大虾看看,这个方法存在的问题
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()">
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没有报错。
当你的鼠标经过某个特定的按钮或者区域
按下Esc按键会有什么效果、
一直不明白楼主所说的
当你的鼠标经过某个特定的按钮或者区域
按下Esc按键会有什么效果、--------------------------------
我写了个例子,在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>
我写了个例子,在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>