ใช้ onkeydown คุณลักษณะ. แอตทริบิวต์ onkeydown จะทริกเกอร์เมื่อผู้ใช้กดปุ่ม
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อรันสคริปต์เมื่อกดปุ่ม −
<!DOCTYPE html>
<html>
<body>
<p>Press a key inside the textbox.</p>
<input type = "text" onkeydown = "display()">
<script>
function display() {
alert("You pressed a key!");
}
</script>
</body>
</html>