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

คุณสมบัติ event.shiftKey ช่วยให้ ทราบว่าปุ่ม Shift ถูกกดขณะเกิดเหตุการณ์หรือไม่

ไวยากรณ์

javascriptevent.shiftKey;

ตัวอย่าง

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