คุณสมบัติ event.altKey

คุณสมบัติ event.altKey ช่วยให้ตรวจสอบได้ว่า ปุ่ม Alt ถูกกดในระหว่างเหตุการณ์หรือไม่

ไวยากรณ์

javascriptevent.altKey;

ตัวอย่าง

ในตัวอย่างต่อไปนี้ เมื่อคลิกที่ปุ่ม เราจะ แสดงข้อความว่า มีการกดปุ่มใดปุ่มหนึ่งในบรรดา Ctrl, Alt หรือ Shift หรือไม่:

html<button id="button">click me</button>
javascriptlet button = document.querySelector('#button');

button.addEventListener('click', function(event) {
  if (event.ctrlKey) {
    alert('กด Ctrl');
  }
  
  if (event.altKey) {
    alert('กด Alt');
  }
  
  if (event.shiftKey) {
    alert('กด Shift');
  }
});

Результат:

ดูเพิ่มเติม

  • คุณสมบัติ event.ctrlKey,
    ซึ่งตรวจจับการกดปุ่ม Ctrl
  • คุณสมบัติ event.shiftKey,
    ซึ่งตรวจจับการกดปุ่ม Shift
  • คุณสมบัติ event.metaKey,
    ซึ่งตรวจจับการกดปุ่ม Cmd