Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中启动键盘快捷键功能?_Javascript_Twitter Bootstrap_Keyboard Shortcuts - Fatal编程技术网

如何在JavaScript中启动键盘快捷键功能?

如何在JavaScript中启动键盘快捷键功能?,javascript,twitter-bootstrap,keyboard-shortcuts,Javascript,Twitter Bootstrap,Keyboard Shortcuts,我的网站上有一个记录功能。如果用户点击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&g

我的网站上有一个记录功能。如果用户点击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>

如果使用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));
}
 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);
 }
您可以使用快捷方式触发keydown和keypress:

shortcut({ctrl: true, alt: true, which: 'r'});
如果您有做不同事情的侦听器(我在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));
}
 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);
 }

谢谢jc ubic。。但我想和你的答案正好相反。。如果我运行record_session();然后必须触发ctrl+alt+r。。如何实现此目的???@karthikeyavavaidya您可以添加标志,在按键中进行测试,并在录制会话中进行设置。我已经更新了答案。