Javascript จับการกดปุ่มบน Keyboard

บางครั้งเวลาทำระบบก็มักต้องการให้มี คีย์ลัด (Shortcut Key) เพื่อช่วยให้เว็บเราใช้งานง่ายขึ้น ตัวอย่างเช่น กดปุ่ม ESC แล้วให้ออกจากระบบ หรือปุ่ม F1 ให้แสดงเมนูช่วยเหลือ เป็นต้น ตรงนี้แหล่ะที่เราจะต้องรู้ว่าเรากดปุ่มอะไรลงไป ซึ่งเราจะใช้เมธอด .KeyCode ของ javascript ช่วยให้รู้ว่า KeyCode ของแต่ล่ะปุ่มคืออะไร
Head

<script language="JavaScript">
document.onkeydown = checkKeycode //กดปุ่มเมื่อไหร่ก็เรียกฟังก์ชั่น checkKeycode เล่ย!

function checkKeycode(e) {

var keycode;

if (window.event) keycode = window.event.keyCode; // ใช้ IE อ่ะ

else if (e) keycode = e.which; // ใช้ Firefox เน่าะ

alert("keycode: " + keycode); //เด้ง popup มาดูดิ๊ว่า key code คืออะไร

}

</script>

ตัวอย่างไฟล์คลิกที่นี่

You can leave a response, or trackback from your own site. Responses are currently closed, but you can trackback from your own site. You can skip to the end and leave a response. Pinging is currently not allowed. Both comments and pings are currently closed.
Powered by WordPress | Shop Free T-Mobile Phones Online | Thanks to Verizon Phones without Contract, MMORPG Games and Sell Car