function simulateKeyPress() {
var evt = document.createEvent("KeyboardEvent");
evt.initKeyboardEvent("keypress", true, true, window,
0, 0, 0, 0,
0, "e".charCodeAt(0))
var body = document.body;
var canceled = !body.dispatchEvent(evt);
if(canceled) {
// A handler called preventDefault
alert("canceled");
} else {
// None of the handlers called preventDefault
alert("not canceled");
}
}
function triggerClick() {
var event = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': true
});
var cb = document.querySelector('input[type=submit][name=btnK]');
var canceled = !cb.dispatchEvent(event);
if (canceled) {
// preventDefault was called and the event cancelled
} else {
// insert your event-logic here...
}
}
let element = document.querySelector('input');
element.onkeydown = e => console.log('keydown on element: ' + e.key);
document.onkeydown = e => console.log('keydown on document: ' + e.key + " " + e.target);
dispatchButton.onclick = () => dispatchKey(element, 'a')
dispatchKey = (target, key) => {
let dom = document;
// focus (dispatched on the DOM with the target set)
let ev = new Event('focus', {target: target});
// this part seems to not work? when logging the
// target in the onkeypress function it shows `document` instead of `input`
// I also tried these and got the same behavior
// ev.target = target;
// and
// Object.defineProperty(ev, 'target', {writable: false, value: target});
dom.dispatchEvent(ev);
// keydown (dispatched on the DOM)
dom.dispatchEvent(new KeyboardEvent('keydown', {'key': key, target: target}));
// beforeinput (dispatched at the target if it's a textarea or input)
target.dispatchEvent(new Event('beforeinput'));
// keypress (dispatched on the DOM)
dom.dispatchEvent(new KeyboardEvent('keypress', {'key': key}));
// input (dispatched at the target if it's a textarea or input)
target.dispatchEvent(new Event('input'));
// change (dispatched at the target if it's a select)
// keyup (dispatched on the DOM)
dom.dispatchEvent(new KeyboardEvent('keyup', {'key': key}));
// Then, optionally for most:
// blur (dispatched on the DOM with the target set)
dom.dispatchEvent(new Event('blur', {target: target}));
console.log('dispatched');
}
<input/>
<button id="dispatchButton">Press to dispatch events </button>
Node.prototype.fire=function(type,options){
var event=new CustomEvent(type);
for(var p in options){
event[p]=options[p];
}
this.dispatchEvent(event);
}
4 ex想模拟ctrl+z
window.addEventListener("keyup",function(ev){
if(ev.ctrlKey && ev.keyCode === 90) console.log(ev); // or do smth
})
document.fire("keyup",{ctrlKey:true,keyCode:90,bubbles:true})
element.dispatchEvent(new KeyboardEvent("keydown", {
key: "e",
keyCode: 69, // example values.
code: "KeyE", // put everything you need in this object.
which: 69,
shiftKey: false, // you don't need to include values
ctrlKey: false, // if you aren't going to use them.
metaKey: false // these are here for example's sake.
}));
document.dispatchEvent(
new KeyboardEvent("keydown", {
key: "e",
keyCode: 69, // example values.
code: "KeyE", // put everything you need in this object.
which: 69,
shiftKey: false, // you don't need to include values
ctrlKey: false, // if you aren't going to use them.
metaKey: false // these are here for example's sake.
})
);
var x = document.querySelector('input');
var keyboardEvent = new KeyboardEvent("keypress", { bubbles: true });
// you can try charCode or keyCode but they are deprecated
Object.defineProperty(keyboardEvent, "key", {
get() {
return "Enter";
},
});
x.dispatchEvent(keyboardEvent);
{
// example
document.querySelector('input').addEventListener("keypress", e => console.log("keypress", e.key))
// unfortunatelly doesn't trigger submit
document.querySelector('form').addEventListener("submit", e => {
e.preventDefault();
console.log("submit")
})
}
var x = document.querySelector('input');
var keyboardEvent = new KeyboardEvent("keypress", { bubbles: true });
// you can try charCode or keyCode but they are deprecated
Object.defineProperty(keyboardEvent, "key", {
get() {
return "Enter";
},
});
x.dispatchEvent(keyboardEvent);
var element = document.getElementById("firstInput");
document.addEventListener("keydown", function(event) {
console.log('we got key:', event.key, ' keyCode:', event.keyCode, ' charCode:', event.charCode);
/* enter is pressed */
if (event.keyCode == 13) {
console.log('enter pressed:', event);
theKey = 'Tab';
attributes = {
bubbles: true,
key: theKey,
keyCode: 9,
charCode: 0,
};
setTimeout(function() {
/* event.keyCode = 13; event.target.value += 'b'; */
var e = new window.KeyboardEvent('focus', attributes);
document.activeElement.dispatchEvent(e);
e = new window.KeyboardEvent('keydown', attributes);
document.activeElement.dispatchEvent(e);
e = new window.KeyboardEvent('beforeinput', attributes);
document.activeElement.dispatchEvent(e);
e = new window.KeyboardEvent('keypress', attributes);
document.activeElement.dispatchEvent(e);
e = new window.KeyboardEvent('input', attributes);
document.activeElement.dispatchEvent(e);
e = new window.KeyboardEvent('change', attributes);
document.activeElement.dispatchEvent(e);
e = new window.KeyboardEvent('keyup', attributes);
document.activeElement.dispatchEvent(e);
}, 4);
setTimeout(function() {
var e = new window.KeyboardEvent('focus', attributes);
document.dispatchEvent(e);
e = new window.KeyboardEvent('keydown', attributes);
document.dispatchEvent(e);
e = new window.KeyboardEvent('beforeinput', attributes);
document.dispatchEvent(e);
e = new window.KeyboardEvent('keypress', attributes);
document.dispatchEvent(e);
e = new window.KeyboardEvent('input', attributes);
document.dispatchEvent(e);
e = new window.KeyboardEvent('change', attributes);
document.dispatchEvent(e);
e = new window.KeyboardEvent('keyup', attributes);
document.dispatchEvent(e);
}, 100);
} else if (event.keyCode != 0) {
console.log('we got a non-enter press...: :', event.key, ' keyCode:', event.keyCode, ' charCode:', event.charCode);
}
});
<h2>convert each enter to a tab in JavaScript... check console for output</h2>
<h3>we dispatchEvents on the activeElement... and the global element as well</h3>
<input type='text' id='firstInput' />
<input type='text' id='secondInput' />
<button type="button" onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>
<p id="demo"></p>