Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a shortcut keys for a specific buttons on the webpage | chrome

Assume that i have two pages: Client page

Items page

in both pages i have an Add buttons, is it possible to create a shortcut keys like ctrl + A will click on the Add button and ctrl + B will be for submitting and ctrl + E for new Entity ?

like image 355
Ali Al Amine Avatar asked Dec 27 '17 11:12

Ali Al Amine


2 Answers

A simple way to detect multiple keydowns to use as shortcuts:

let keysDown = {};
window.onkeydown = function(e) {
  keysDown[e.key] = true;

  if (keysDown["Control"] && keysDown["a"]) {
    //do what you want when control and a is pressed for example
    console.log("control + a");
  }
  else if( keysDown["Control"] && keysDown["b"] ){
    console.log("control + b");
  }
}

window.onkeyup = function(e) {
  keysDown[e.key] = false;
}
like image 142
Dwadelfri Avatar answered Nov 14 '22 21:11

Dwadelfri


Yes, this is definitely possible!

You can build the code yourself, but there is no point in reinventing the wheel, so try one of pre-build libraries. For example: https://craig.is/killing/mice

You can create custom shortcuts and bind them to javascript functions. These functions will than process the action.

Good luck!

Also see: How can I add a JavaScript keyboard shortcut to an existing JavaScript Function?

like image 22
Sam Kool Avatar answered Nov 14 '22 23:11

Sam Kool