Difference between keypress() and keydown() events

Press any key to check the event and the keycode. Non-printing keys like Esc, Shift key , Delete key etc are not triggered by keypress() event.
keydown() event is case insensitive
keypress() event is case sensitive

Press any char key and see the difference between keycodes, keep the caps Lock in on position and check again.
keydown event details here
keypress event details here


If you are using arrow keys then you must use keydown() event as keypress() event can't sesnse arrow key events.
event.which : will display the keycode or ASCII value of the key.
Full Source code is here

JQuery

<script>
$(document).ready(function() {
//// keydown() ///
$(document).keydown(function(){
$('#d1').html('Keydown Event happened, keycode: '+ event.which);
$("#d1").show();
setTimeout(function() { $("#d1").html('Press any key'); }, 2000);
});
///////keypress() //
$(document).keypress(function(){
$('#d2').html('Keypress Event happened, keycode: '+ event.which);
$("#d2").show();
setTimeout(function() { $("#d2").html('Press any key'); }, 2000);
});
///
});
</script>

HTML

<div id=d1 class='alert alert-primary'>keydown event details here</div>
 <div id=d2 class='alert alert-primary'>keypress event details here</div>

Event Reference
keydown events Learn which key is pressed by user

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here .







    Most Popular JQuery Scripts

    1

    Two dependant list boxes

    2

    Calendar with Date Selection

    3

    Data change by Slider

    4

    Show & Hide element


    JQuery Video Tutorials




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer