With the help of a fellow Stack Overflow member I got a code for having shortcuts using Keydown.
How to Check if jQuery .keydown() has any other keys pressed or not?
The code uses setTimeout function for every single keydown event. I am using it on a page where users usually type more than 1000 words. (4k-5k characters I assume)
This means Keydown would run on all those 5k keydowns and new timeouts will starts many times.
var NewKeys = {};
var NewTimeout;
jQuery(document).keydown(function (e) {
if (NewTimeout) {
clearTimeout(NewTimeout);
}
NewKeys[e.which] = true;
NewTimeout = setTimeout(function () {
if (typeof NewKeys[17] !== "undefined" && typeof NewKeys[83] !== "undefined" && Object.keys(NewKeys).length === 2) {
e.preventDefault();
if(!jQuery('.SearchCreations').hasClass('Show')){
e.preventDefault();
jQuery('.SaveCreation').click();
}
}
if (typeof NewKeys[27] !== "undefined" && Object.keys(NewKeys).length === 1) {
jQuery('.Thumbnail.Close').click();
jQuery('.Action').removeClass('Collapse');
}
if (typeof NewKeys[16] !== "undefined" && typeof NewKeys[17] !== "undefined" && typeof NewKeys[83] !== "undefined" && Object.keys(NewKeys).length === 3) {
e.preventDefault();
if(jQuery('.CreationEditor').hasClass('Autosaving')){
jQuery('.StopSave').click();
}
else{
jQuery('.AutoSave').click();
}
}
}, 200);
});
Can this affect performance/reliability for the end user.
The last thing you want to do is have a webpage that is so resource intensive that your user's device stops working properly.
I tried this code on 6-7 year old laptop (i5, 4GB RAM, Windows 10). I noticed that if I keep using the page it works fine. But if I switch window to some other application using
Alt + Taband return back to browser in about 5-7 sec to my site, the Shortcut keys would stop working.
Can confirm this behavior is consistent, and it is reproducable on multiple Windows 10 machines as of now.
This pretty much depends on your timeout value and also the speed of your typing. For example your timeout is 3 and you type 5 letters in second. Then you will end up with 15 functions that run simultaneously.
It's fine, but when you use a longer timeout, like maybe 10s and your speed is 10 letters/sec, then you will have 100 functions running at the same time which is not appropriate.
Specially if you try to do heavier tasks than changing classes and some DOM manipulations.