본문 바로가기

자바스크립트

[JavaScript] 81. keyup keypress keydown 차이

1) keyup: happens when we lift or finger off the keyboard or off the key

    키보드에서 손을 땠을 때

2) keypress: when we keep our finger on a certain key

     키보드를 눌렀을 때, 누르고 있을 때 계속 실행

3) keydown: as soon as we just press down the key. so ususally that is the one that we use

   키보드를 눌렀을 때, 누르고 있을 떄 한번 실행 -> 대부분 keydown 사용

document.addEventListener('keydown', function (e) {
  //   console.log(e.key);
  if (e.key === 'Escape' && !modal.classList.contains('hidden')) {
    closeModal();
  }
});