Quick Reach
When jQuery keydown event occurs?
The keydown event is triggered when a key is pressed down from the keyboard. The keydown() method will be used to perform any action or run a function as keypress event occurs in your web document.
Keydown event is quite similar to keypress event, however, there are differences between the two – for example pressing a few keys will trigger only keydown event like ALT, CTRL, ESC etc. keypress event will not be triggered for those keys.
Also, the keypress event returns different values for lowercase and uppercase letters, whereas the keyup and keydown return same.
Syntax of keydown jQuery method
$(selector).keydown(function)
Where function is optional, to execute as keydown event occurs.
Running example of keydown event
The example below shows when a key is pressed down and the keydown event occurs, the keydown() method will capture the event and throw an alert with ASCII value of the pressed key.
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<html>
<head>
<script src=“http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js”></script>
<script>
$(document).ready(function(){
$(“#keyboard”).keydown(function(){
alert(“keydown event occured! The Ascii value of pressed key is: “ + event.keyCode);
});
});
</script>
</head>
<body>
Enter keyboard keys: <input type=“text” id=“keyboard”>
</body>
</html>
|
Related : keypress() event | keyup() event
Leave A Comment?