돌아가기
이 글은 다음 언어로만 작성되어 있습니다. English, Español, Français, Italiano, 日本語, Русский, Türkçe, Українська, 简体中文. 한국어 번역에 참여해주세요.

Extended hotkeys

중요도: 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)

runOnKeys(
  () => alert("Hello!"),
  "KeyQ",
  "KeyW"
);

새 창에서 데모 보기

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

Let’s create a set pressed = new Set() to 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.

샌드박스를 열어 정답을 확인해보세요.