简体   繁体   English

鼠标点击模拟 JavaScript 中的键盘按键

[英]Mouse click simulates keyboard key in JavaScript

I have situation that in application (HTML/XUL) there are key bindings for some function keys ( F5 , F7 & F9 ) in order to communicate with some other application.我的情况是,在应用程序(HTML/XUL)中有一些功能键( F5F7F9 )的键绑定,以便与其他应用程序进行通信。

Now I have to make touch-based interface for same app, without keyboard and advanced touch events.现在我必须为同一个应用程序制作基于触摸的界面,没有键盘和高级触摸事件。 All touch clicks are actually same as mouse clicks and I need to have 3 buttons/links that behave same as F5 , F7 & F9 keys.所有触摸点击实际上与鼠标点击相同,我需要有 3 个按钮/链接,其行为与F5F7F9键相同。

Don't ask for reasons, all I can say is that I need to keep that key bindings.不要问原因,我只能说我需要保留那些键绑定。

Can I assign mouse click on link to act like F-key is pressed?我可以在链接上分配鼠标单击以充当按下 F 键的动作吗?

In Jake's answer is most of solution and based on that I gave the whole answer, because is not so easy on first glance for beginners to modify functionality.在 Jake 的答案中,大部分解决方案都是基于此,我给出了完整的答案,因为对于初学者来说,修改功能乍一看并不容易。 This is modified function from this thread :这是来自该线程的修改函数:

function simulateKeyPress(target, options) {
    var event = target.ownerDocument.createEvent('KeyEvents'),
        options = options || {};

    // Set your options or || default values
    var opts = {
        type: options.type                  || "keypress",
        bubbles: options.bubbles            || true,
        cancelable: options.cancelable      || true,
        viewArg: options.viewArg            || null,
        ctrlKeyArg: options.ctrlKeyArg      || false,
        altKeyArg: options.altKeyArg        || false,
        shiftKeyArg: options.shiftKeyArg    || false,
        metaKeyArg: options.metaKeyArg      || false,
        keyCodeArg: options.keyCodeArg      || 0,
        charCodeArg: options.charCodeArg    || 0
    }

    // Pass in the options
    event.initKeyEvent(
        opts.type,
        opts.bubbles,
        opts.cancelable,
        opts.viewArg,
        opts.ctrlKeyArg,
        opts.altKeyArg,
        opts.shiftKeyArg,
        opts.metaKeyArg,
        opts.keyCodeArg,
        opts.charCodeArg
    );

    // Fire the event
    target.dispatchEvent(event);
    event.stopPropagation;
}

And now we call it on desired element/event for desired key to be pressed.现在我们在所需的元素/事件上调用它来按下所需的键。 Second argument is object with all options changeable (in my case keyCodeArg: 116 is only needed to be send to simulate F5 key press).第二个参数是所有选项均可更改的对象(在我的情况下,keyCodeArg: 116 只需要发送以模拟 F5 按键)。

document.getElementById(element).addEventListener('click', function() {
    simulateKeyPress(this, {keyCodeArg: 116});
});

Mozilla Developer Network has nice articles about KeyboardEvents and initKeyEvent . Mozilla Developer Network 有关于KeyboardEventsinitKeyEvent 的好文章。

There are two ways to do this I guess.我想有两种方法可以做到这一点。

The simple way would be to have your F-keys call a function, and simply call the same function when catching a mouse event.简单的方法是让您的 F 键调用一个函数,并在捕获鼠标事件时简单地调用相同的函数。

The other way would be to catch the mouse event, and fire a F-key press event.另一种方法是捕获鼠标事件,并触发 F 键按下事件。 Then it would pretty much be a duplicate of this question:那么它几乎是这个问题的重复:

How to simulate a mouse click using JavaScript? 如何使用 JavaScript 模拟鼠标点击?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM