Back to the lesson

Extended hotkeys

importance: 5

Create a function runOnKeys(func, code1, code2, ... code_n) that runs func on simultaneous pressing of keys with codes code1, code2, …, code_n.

For instance, the code below shows alert when "Q" and "W" are pressed together (in any language, with or without CapsLock)

  () => alert("Hello!"),

Demo in new window

We should use two handlers: document.onkeydown and document.onkeyup.

The set pressed should keep currently pressed keys.

The first handler adds to it, while the second one removes from it. Every time on keydown we check if we have enough keys pressed, and run the function if it is so.

Open the solution in a sandbox.