1

I am writing a program where I want to be adding custom hotkeys and store these. The combination must be input by the user. Let's say user selects CTRL + M. How do I store this combination and give an alert message whenever this combination is pressed in Vanilla JS.

ROOT
  • 11,363
  • 5
  • 30
  • 45
  • 2
    Welcome to Stack Overflow! Please take the [tour](/tour) (you get a badge!) and read through the [help center](https://stackoverflow.com/help), in particular [How do I ask a good question?](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your attempt and say specifically where you're stuck. People will be glad to help. – palaѕн Mar 22 '20 at 05:48
  • 1
    Does this answer your question? [how to implement shortcut key combination of CTRL or SHIFT + through javascript?](https://stackoverflow.com/questions/64820/how-to-implement-shortcut-key-combination-of-ctrl-or-shift-letter-through-ja) – Alessandro Mandelli Mar 22 '20 at 08:33

1 Answers1

1

You can use keydown event to detect the keyboard events also check for Ctrl key using event.ctrlKey in the event handler.

update I added radio buttons to show how you can have it dynamically use the combination using keydown event and you can also store it in database and display it like this in your front-end:

document.addEventListener('keydown', logKey);

function logKey(e) {
  let command = document.querySelector('input[name="command"]:checked').value;
  let keypressed = document.querySelector('input[name="keypressed"]:checked').value;
  if (e.keyCode == keypressed && e[command]) alert(` ${command} + ${String.fromCharCode(e.keyCode)} pressed`);
  e.preventDefault();
  return false;
}
<label><input type="radio" name="command" value="shiftKey" checked /> Shift<label><br />
<label><input type="radio" name="command" value="ctrlKey" /> Ctrl<label><br />

<label><input type="radio" name="command" value="altKey" /> Alt<label>

<hr />

<label><input type="radio" name="keypressed" value="77" checked /> M<label><br />
<label><input type="radio" name="keypressed" value="68" /> D<label><br />
<label><input type="radio" name="keypressed" value="83" /> S<label>


<p>Focus the IFrame first (e.g. by clicking in it), then try pressing selected keys.</p>
<p id="log"></p>
ROOT
  • 11,363
  • 5
  • 30
  • 45
  • Thanks...but this works for ctrlKey, yes..However i wanted something where we can choose a combination...like Shift + F, Ctrl + J or anyother 2 key combination..which can be stored in variables and then checked for keydown events...But i dont know how to store ctrlKey as a variable – Vishruth Subramanian Mar 22 '20 at 06:24
  • Ok i will update my answer, you should add that to your question, since its not clear that you want this in the way you are asking. – ROOT Mar 22 '20 at 06:27
  • Can you also add something so that this combination would override default hotkeys...for eg...if i use CTRL + S, it should override the Chrome hotkey for "SAVE" – Vishruth Subramanian Mar 22 '20 at 16:52
  • @VishruthSubramanian, I added the code, also note that I added `e.preventDefault()` and `return false,` to stop the default behavior. – ROOT Mar 22 '20 at 18:59