I have a record functionality in my website. If a user hits Ctrl + Alt + R the recording will begin. Now I would like a button named RECORD
in my html page, so that when a user hits that button recording will start.
<button type="submit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" onclick="record_session()" title="Start Recording">Record</button>
in my function below
function record_session(){
//how can i trigger or initiate ctrl+alt+r here??
}
If you use jQuery you can add keypress event:
$(document).keypress(function(e) {
if (e.which === 114 && e.ctrlKey && e.altKey) {
record_session();
}
});
UPDATE :
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
}
}
});
UPDATE 2
to fire keyboard event you can use this:
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));
}
you can use shortcut to trigger both keydown and keypress:
shortcut({ctrl: true, alt: true, which: 'r'});
in case you have listener that do different things (I have this in my tests for jQuery Terminal);
Native code:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.