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

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

ไวยากรณ์

javascriptevent.ctrlKey;

ตัวอย่าง

ในตัวอย่างต่อไปนี้ เมื่อคลิกที่ปุ่ม เราจะแสดงข้อความแจ้งว่าหนึ่งในปุ่ม 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.altKey,
    ซึ่งตรวจจับการกดปุ่ม Alt
  • คุณสมบัติ event.shiftKey,
    ซึ่งตรวจจับการกดปุ่ม Shift
  • คุณสมบัติ event.metaKey,
    ซึ่งตรวจจับการกดปุ่ม Cmd