参考:https://q.cnblogs.com/q/74438/
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
<!DOCTYPE html>
<html>
<head>
<title>123</title>
</head>
<body>
<input type="text" id="inputID1" />
<script>
function syf(target, type)
{
target.focus();
var doc = target.ownerDocument;
//创建键盘事件对象
var customEventObject = undefined;
customEventObject = doc.createEvent("UIEvents"); //or eventType = HtmlEvents
customEventObject.initEvent(type, true, true);
customEventObject.view = null;
customEventObject.altKey = false;
customEventObject.ctrlKey = false;
customEventObject.shiftKey = false;
customEventObject.metaKey = false;
target.dispatchEvent(customEventObject)
}
//模拟
function simulateKeyPress(target, type, keycode, charcode) {
target.focus();
var doc = target.ownerDocument;
var Event = doc.defaultView.Event;
//创建键盘事件对象
var customEventObject = undefined;
try {
try {
customEventObject = new Event(type);
} catch (e) {
customEventObject = doc.createEvent("HTMLEvents");
}
customEventObject.initEvent(type, true, true);
customEventObject.shiftKey = false;
customEventObject.altKey = false;
customEventObject.metaKey = false;
customEventObject.ctrlKey = false;
customEventObject.keyCode = keycode;
customEventObject.charCode = charcode;
customEventObject.which = customEventObject.keyCode;
// if( customEventObject.initKeyEvent ){
// customEventObject.initKeyEvent(type, true ,true,document.defaultView , false ,false ,false ,false ,0, parseInt('A',10) );
// }else{
// console.log("------------ initKeyboardEvent---------------------");
// customEventObject.initKeyboardEvent(type,true ,true,document.defaultView , false ,false ,false ,false ,0, parseInt('A',10) );
// }
} catch (exception) {
console.log(exception);
try {
customEventObject = doc.createEvent('Events');
} catch (uiException) {
customEventObject = doc.createEvent("UIEvents"); //or eventType = HtmlEvents
} finally {
customEventObject.initEvent(type, true, true);
customEventObject.view = null;
customEventObject.altKey = false;
customEventObject.ctrlKey = false;
customEventObject.shiftKey = false;
customEventObject.metaKey = false;
customEventObject.keyCode = keycode;
customEventObject.charCode = charcode;
}
}
target.dispatchEvent(customEventObject)
};
// test code
// 随便写个html,放个input(type=text) ,然后监听该input的keydown或者keypress事件查看下是否被触发就可以了
var txtobject = document.getElementById("inputID1");
/*
txtobject.addEventListener('input', function (e) {
console.log("successful input");
console.log(e.keyCode);
//console.log(e.charCode);
txtobject.value = e.charCode;
}, false);
*/
txtobject.addEventListener('keydown', function (e) {
console.log("successful keydown");
console.log(e.keyCode);
//console.log(e.charCode);
//txtobject.value = e.charCode;
}, false);
function test() {
//txtobject.value="what";
syf(txtobject,'keydown','105', '0');
}
</script>
<button οnclick="test()" style="width:100px;height:30px" value="tt"></button>
<script type="text/javascript">
/*function syf1(target, type){target.focus();var doc = target.ownerDocument;var customEventObject = undefined;customEventObject = doc.createEvent("UIEvents");customEventObject.initEvent(type, true, true);customEventObject.view = null;customEventObject.altKey = false;customEventObject.ctrlKey = false;customEventObject.shiftKey = false;customEventObject.metaKey = false;target.dispatchEvent(customEventObject)}var txtobject = document.getElementById("inputID1");txtobject.value="what";syf1(txtobject,'input','105', '0');
*/
</script>
</body>
</html>