Bu örneğimizde jQuery ile klavyeden basılan tuşun kodunu almayı göreceğiz. Kullanıcının bastığı tuşa göre işlem yapmak istediğimizde kullanabileceğimiz tuş kodlarını örneğimizde alert ile mesaj olarak göreceğiz.
Sayfamızı çalıştırıp bir tuşa basalım. Örneğin ESC tuşu.
Şimdiki örneğimizde de kullanıcının Enter tuşuna basması durumunda bir mesaj verelim.
Sayfamız açıldığında Enter tuşuna bastığımızda;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <html> <head> <meta charset="utf-8"> <title>www.yazilimkodlama.com</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body> <script> $(document).on("keydown", function(e){ alert("Basılan tuş kodu : "+ e.keyCode); }); </script> </body> </html> |

Şimdiki örneğimizde de kullanıcının Enter tuşuna basması durumunda bir mesaj verelim.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <!DOCTYPE html> <html lang=""> <head> <meta charset="utf-8"> <title>yazilimkodlama.com</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body> <script> $(document).on("keydown", function(e){ if ( e.keyCode == 13 ){ //return false; //enter e basıldığinda hiçbir işlem yaptırmak istemiyorsanız. alert("Enter tuşuna basıldı"); } }); </script> </body> </html> |
