我的网站上有记录功能。如果用户按 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??
}
答案 0 :(得分:2)
如果你使用jQuery,你可以添加keypress事件:
$(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);
}