I want to automatically trigger a keydown
event after specific amount of time has passed. I set up a timout event for this with the code below:
setTimeout(
function() {
document.querySelector("body").dispatchEvent(new KeyboardEvent('keydown', {
keyCode: 83,
altKey: true
}));
},
12000
);
However, it doesn't seem to trigger anything. Could anyone please help me in figuring out how can i send a keydown event in such cases?
I just tried your code and it triggers alright:
document.querySelector("body").addEventListener('keydown', (e) => { console.log('triggered', e) }) setTimeout( function() { document.querySelector("body").dispatchEvent(new KeyboardEvent('keydown', { keyCode: 83, altKey: true })); }, 2000 );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.