คุณสมบัติ event.metaKey
คุณสมบัติ event.metaKey ช่วยให้ตรวจสอบได้ว่า
ปุ่ม Cmd ถูกกดในระหว่างเหตุการณ์หรือไม่
หากถูกกด คุณสมบัติจะส่งคืนค่า true
ในกรณีอื่นๆ - false
ไวยากรณ์
event.metaKey;
ตัวอย่าง
ในตัวอย่างต่อไปนี้ เมื่อคลิกที่ปุ่ม
จะแสดงข้อความว่าปุ่ม Cmd หรือปุ่ม Ctrl, Alt, Shift ถูกกดหรือไม่:
<button id="button">click me</button>
let button = document.querySelector('#button');
button.addEventListener('click', function(event) {
if (event.metaKey) {
alert('Cmd ถูกกด');
}
if (event.ctrlKey) {
alert('Ctrl ถูกกด');
}
if (event.altKey) {
alert('Alt ถูกกด');
}
if (event.shiftKey) {
alert('Shift ถูกกด');
}
});
:
ดูเพิ่มเติม
-
คุณสมบัติ
event.ctrlKey,
ซึ่งตรวจจับการกดปุ่ม Ctrl -
คุณสมบัติ
event.shiftKey,
ซึ่งตรวจจับการกดปุ่ม Shift -
คุณสมบัติ
event.altKey,
ซึ่งตรวจจับการกดปุ่ม Alt -
คุณสมบัติ
code,
ซึ่งรับรหัสของปุ่มที่ถูกกด -
คุณสมบัติ
key,
ซึ่งรับอักขระที่ป้อนเข้ามา