.keydown()
Syntax and Description
.keydown(handler)
.keydown()
handler
is a function to execute
each time the event is triggered.
Return value is the jQuery object,
for chaining purposes.
keydown
binds an event handler to the
keydown
JavaScript event, or trigger that
event on an element.
.keydown(handler)
is a shortcut for
.bind('keydown', handler)
.
.keydown()
is a shortcut for and
.trigger('keydown')
.
We can trigger the event manually when another element is clicked.
$('#other').click(function() {
$('#target').keydown();
});
Listen to keydown event
The following code listens to keydown event and displays the key code for the key pressed.
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){<!-- www . j a v a 2 s . c o m-->
$(window).keydown(function(event){
alert(event.keyCode);
});
});
</script>
</head>
<body>
<body>
<p><input type="text" /> <span>ja va2s.com</span></p>
</body>
</html>
The code above generates the following result.
Javascript Tutorial Event
Event
Event attributes
event.keycode
event.clientX/clientY
pageX/pageY
Cancel event
Event target
bind
.blur()
.change()
.click()
.error()
.dblclick()
.delegate()
.die()
.focus()
.hover()
.keyup()
.live()
.load()
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseover()
.mouseout()
.mouseup()
.off()
.on()
.one()
.ready()
.resize()
.scroll()
.select()
.submit()
.toggle()
.trigger()
.triggerHandler()
.unbind()
.undelegate()
.unload()
Event
Event attributes
event.keycode
event.clientX/clientY
pageX/pageY
Cancel event
Event target
bind
.blur()
.change()
.click()
.error()
.dblclick()
.delegate()
.die()
.focus()
.hover()
.keydown()
.keypress().keyup()
.live()
.load()
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseover()
.mouseout()
.mouseup()
.off()
.on()
.one()
.ready()
.resize()
.scroll()
.select()
.submit()
.toggle()
.trigger()
.triggerHandler()
.unbind()
.undelegate()
.unload()