admin管理员组文章数量:1297112
is it possible to trigger a key event on the DOMWindow or DOMDocument in javascript? Basically I am creating an browser extension to interact with a website and they have shortcut Key Events (similar to GMail) on performing specific actions. I have found other postings on how to properly trigger key events (Definitive way to trigger keypress events with jQuery) but they don't seem to work when sending the key events to the document/window.
So far I have tried:
var evt = document.createEvent("KeyboardEvent");
evt.initKeyboardEvent("keydown", true, true, window, false, false, false, false, 0, "o".charCodeAt(0))
window.document.dispatchEvent(evt);
and a jQuery implementation:
$(document).trigger({ type: 'keydown', which: "0".charCodeAt(0) });
I also tried doing "keydown", "keypress" and "keyup" in succession but that did not work either.
Any help would be greatly appreciated!
Thanks in advance.
is it possible to trigger a key event on the DOMWindow or DOMDocument in javascript? Basically I am creating an browser extension to interact with a website and they have shortcut Key Events (similar to GMail) on performing specific actions. I have found other postings on how to properly trigger key events (Definitive way to trigger keypress events with jQuery) but they don't seem to work when sending the key events to the document/window.
So far I have tried:
var evt = document.createEvent("KeyboardEvent");
evt.initKeyboardEvent("keydown", true, true, window, false, false, false, false, 0, "o".charCodeAt(0))
window.document.dispatchEvent(evt);
and a jQuery implementation:
$(document).trigger({ type: 'keydown', which: "0".charCodeAt(0) });
I also tried doing "keydown", "keypress" and "keyup" in succession but that did not work either.
Any help would be greatly appreciated!
Thanks in advance.
Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Dec 15, 2011 at 20:19 thiesdiggitythiesdiggity 1,9672 gold badges20 silver badges29 bronze badges3 Answers
Reset to default 6I know you can pass extra arguments when using trigger()
but I'm not sure you can overwrite properties of the event
object. You can pass data like this though:
$(document).on('keydown keyup keypress', function (event, characterCode) {
if (typeof(characterCode) == 'undefined') {
characterCode = -1;
}
console.log('type = ' + event.type);
console.log('characterCode = ' + characterCode);
});
And this would be the trigger()
code:
$(document).trigger('keydown', [ "0".charCodeAt(0) ]);
Here is a demo: http://jsfiddle/A7cEE/ (watch your console for the logs)
The code from the stackoverflow answer you provided seems to work fine:
To Capture:
$(document).on('keydown', function (event) {
console.log('type = ' + event.type);
console.log('keyCode = ' + event.keyCode);
});
To Trigger:
var e = jQuery.Event('keydown');
e.keyCode = "0".charCodeAt(0);
$(document).trigger(e);
Demo: http://jsfiddle/A7cEE/1/
Note that .on()
is new in jQuery 1.7 and is the same as .bind()
in this case.
Try this:
var evt = document.createEvent("KeyboardEvent");
evt.initKeyEvent ("keydown", true, true, window, false, false, false, false, 0, "o".charCodeAt(0))
window.document.dispatchEvent(evt);
syntax:
event.initKeyEvent (type, bubbles, cancelable, viewArg,
ctrlKeyArg, altKeyArg, shiftKeyArg, metaKeyArg,
keyCodeArg, charCodeArg)
Use "initKeyEvent" instead of "initKeyboardEvent".
//::jQuery::
//press "L" or "l" to open Bootstrap Login Modal Form
$(document).keypress(function(evt){
if (evt.charCode === 108 || evt.charCode == 76) {
$('#login-modal')
.modal('bs.modal.shown');
}
});
Good Luck.
本文标签: Is it possible to trigger a key event on the DOM Window with JavascriptJQueryStack Overflow
版权声明:本文标题:Is it possible to trigger a key event on the DOM Window with JavascriptJQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741604032a2387865.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论