是否可以使用 Javascript/JQuery 在 DOM 窗口上触发按键事件

发布于 2024-12-21 16:49:20 字数 740 浏览 5 评论 0原文

是否可以在 javascript 中触发 DOMWindow 或 DOMDocument 上的按键事件?基本上,我正在创建一个浏览器扩展来与网站交互,并且它们具有执行特定操作的快捷键事件(类似于 GMail)。我找到了有关如何正确触发关键事件的其他帖子(Definitive way使用 jQuery 触发按键事件),但在将按键事件发送到文档/窗口时它们似乎不起作用。

到目前为止,我已经尝试过:

var evt = document.createEvent("KeyboardEvent");
evt.initKeyboardEvent("keydown", true, true, window, false, false, false, false, 0, "o".charCodeAt(0))
window.document.dispatchEvent(evt);

和 jQuery 实现:

$(document).trigger({ type: 'keydown', which: "0".charCodeAt(0) });

我还尝试连续执行“keydown”、“keypress”和“keyup”,但这也不起作用。

任何帮助将不胜感激!

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过潦 2024-12-28 16:49:21

我知道您可以在使用 trigger() 时传递额外的参数,但我不确定您是否可以覆盖 event 对象的属性。您可以像这样传递数据:

$(document).on('keydown keyup keypress', function (event, characterCode) {
    if (typeof(characterCode) == 'undefined') {
        characterCode = -1;
    }
    console.log('type = ' + event.type);
    console.log('characterCode = ' + characterCode);
});

这将是 trigger() 代码:

$(document).trigger('keydown', [ "0".charCodeAt(0) ]);

这是一个演示:http://jsfiddle.net/A7cEE/ (观察控制台的日志)

您提供的 stackoverflow 答案中的代码似乎工作正常:

捕获:

$(document).on('keydown', function (event) {
    console.log('type = ' + event.type);
    console.log('keyCode = ' + event.keyCode);
});

触发:

var e = jQuery.Event('keydown');
e.keyCode = "0".charCodeAt(0);
$(document).trigger(e);

演示:http://jsfiddle.net/A7cEE/1/

请注意 .on() 是 jQuery 1.7 中的新功能,在本例中与 .bind() 相同。

I 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.net/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.net/A7cEE/1/

Note that .on() is new in jQuery 1.7 and is the same as .bind() in this case.

帅哥哥的热头脑 2024-12-28 16:49:21

试试这个:

var evt = document.createEvent("KeyboardEvent");
evt.initKeyEvent ("keydown", true, true, window, false, false, false, false, 0, "o".charCodeAt(0))
window.document.dispatchEvent(evt);

语法:

event.initKeyEvent (type, bubbles, cancelable, viewArg, 
                    ctrlKeyArg, altKeyArg, shiftKeyArg, metaKeyArg, 
                    keyCodeArg, charCodeArg) 

使用“initKeyEvent”而不是“initKeyboardEvent”。

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".

旧城空念 2024-12-28 16:49:21
//::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');
  }
});

祝你好运。

//::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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文