Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

คุณสมบัติ metaKey ของ KeyboardEvent ใน HTML DOM

คุณสมบัติ metaKey ของ KeyboardEvent ใช้ตรวจสอบว่าปุ่ม Meta (ปุ่ม ⌘ Command บน Mac หรือปุ่ม Windows บน PC) ถูกกดขณะเกิดเหตุการณ์หรือไม่ (คืนค่า true/false)

ไวยากรณ์ (Syntax)

event.metaKey

ตัวอย่างการใช้งาน

ตัวอย่างด้านล่างท้าทายผู้ใช้ให้กดปุ่ม Meta:

<!DOCTYPE html>
<html>
<head>
<title>KeyboardEvent metaKey</title>
<style>
  form { width: 70%; margin: 0 auto; text-align: center; }
  * { padding: 2px; margin: 5px; }
  input[type="button"] { border-radius: 10px; }
</style>
</head>
<body>
<form>
  <fieldset>
    <legend>KeyboardEvent-metaKey</legend>
    <label>Editor: <input type="text" id="textSelect" onkeydown="getEventData(event)" autocomplete="off"> </label>
    <div id="divDisplay">I Dare you to press the meta key</div>
  </fieldset>
</form>
<script>
  var divDisplay = document.getElementById("divDisplay");
  var textSelect = document.getElementById("textSelect");
  
  function getEventData(InputEvent) {
    if(InputEvent.metaKey === true)
      divDisplay.textContent = 'You are very brave!';
    else
      divDisplay.textContent = 'You scared enough?';
  }
</script>
</body>
</html>

ผลลัพธ์

ก่อนพิมพ์: แสดง "I Dare you to press the meta key"

พิมพ์โดยไม่กด Meta: แสดง "You scared enough?"

กด Meta (⌘/Windows) ขณะพิมพ์: แสดง "You are very brave!"

หมายเหตุ: รูปภาพตัวอย่างจากต้นฉบับไม่ได้รวมอยู่ในเนื้อหานี้