如何在 JavaScript 中触发键盘快捷键功能?

问题描述 投票:0回答:2

我的网站有记录功能。如果用户按下 Ctrl+Alt+R,录音就会开始。现在我想在我的 html 页面中有一个名为

RECORD
的按钮,这样当用户点击该按钮时就会开始录制。

<button type="submit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" onclick="record_session()" title="Start Recording">Record</button>

在我下面的函数中

function record_session(){
    //how can i trigger or initiate  ctrl+alt+r here??
}
javascript twitter-bootstrap keyboard-shortcuts
2个回答
3
投票

如果您使用 jQuery,您可以添加按键事件:

$(document).keypress(function(e) {
    if (e.which === 114 && e.ctrlKey && e.altKey) {
        record_session();
    }
});

更新

var enable_keypress = false;
function record_session(){
    enable_keypress = true;
}
$(document).keypress(function(e) {
    if (enable_keypress) {
        if (e.which === 114 && e.ctrlKey && e.altKey) { // ctrl+alt+r

        }
    }
});

更新2

要触发键盘事件,您可以使用以下命令:

jQuery:

function keydown(ctrl, alt, shift, which, key) {
    var e = $.Event("keydown");
    e.ctrlKey = ctrl;
    e.altKey = alt;
    e.shiftKey = shift;
    if (typeof which === 'string') {
        key = which;
        which = key.toUpperCase().charCodeAt(0);
    }
    e.key = key;
    e.which = e.keyCode = which;
    return e;
}
function keypress(key) {
    var e = $.Event("keypress");
    e.key = key;
    e.which = e.keyCode = 0;
    return e;
}
function shortcut({
    ctrl = false,
    alt = false,
    shift = false,
    which,
    key
}) {
    var doc = $(document.documentElement || window);
    if (typeof which === 'string') {
        key = which;
        which = key.toUpperCase().charCodeAt(0);
    }
    doc.trigger(keydown(ctrl, alt, shift, which, key));
    doc.trigger(keypress(key));
}

您可以使用快捷键来触发 keydown 和 keypress:

shortcut({ctrl: true, alt: true, which: 'r'});

如果你有监听器做不同的事情(我在 jQuery 终端的测试中有这个);

本机代码:

 function shortcut({chr, ctrlKey = false, altKey = false}) {
     var lowerChr = chr.toLowerCase();
     var upperChr = chr.toUpperCase();
     var keydownCode = upperChr.charCodeAt(0);
     var e = new KeyboardEvent("keydown", {
         key: lowerChr,
         code: "Key" + upperChr,
         ctrlKey,
         altKey,
         keyCode: keydownCode,
         which: keydownCode
     });
     var keypressCode = lowerChr.charCodeAt(0);
     document.documentElement.dispatchEvent(e);
     var e = new KeyboardEvent("keypress", {
         key: lowerChr,
         ctrlKey,
         altKey,
         charCode: keypressCode,
         keyCode: keypressCode,
         which: keypressCode
     });
     document.documentElement.dispatchEvent(e);
 }

0
投票

为了在 2024 年使用 vanilla JS 直接回答您的问题,我将根据您的具体要求触发自定义的 KeyboardEvent

function record_session() {
    // Trigger customized keyboard event
    document.documentElement.dispatchEvent(new KeyboardEvent('keydown', {
        key: 'r', // Lowercase letter
        code: 'KeyR', // Upppercase letter prefixed with "Key"
        ctrlKey: true, // boolean, true means CTRL key is pressed
        altKey: true // boolean, true means ALT key is pressed
    }));
}

keyCode
属性已已弃用,因此您不再需要包含它

© www.soinside.com 2019 - 2024. All rights reserved.