Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

101 lines (99 sloc) 5.374 kb
<?xml version="1.0"?>
<entry type="method" name="keypress" return="jQuery">
<title>.keypress()</title>
<desc>Bind an event handler to the "keypress" JavaScript event, or trigger that event on an element.</desc>
<signature>
<added>1.0</added>
<argument name="handler(eventObject)" type="Function">
<desc>A function to execute each time the event is triggered.</desc>
</argument>
</signature>
<signature>
<added>1.4.3</added>
<argument name="eventData" type="PlainObject" optional="true">
<desc>A map of data that will be passed to the event handler.</desc>
</argument>
<argument name="handler(eventObject)" type="Function">
<desc>A function to execute each time the event is triggered.</desc>
</argument>
</signature>
<signature>
<added>1.0</added>
</signature>
<longdesc>
<p><strong>Note:</strong> as the <code>keypress</code> event isn't covered by any official specification, the actual behavior encountered when using it may differ across browsers, browser versions, and platforms.</p>
<p>This method is a shortcut for <code>.bind("keypress", handler)</code> in the first two variations, and <code>.trigger("keypress")</code> in the third.</p>
<p>The <code>keypress</code> event is sent to an element when the browser registers keyboard input. This is similar to the <code>keydown</code> event, except in the case of key repeats. If the user presses and holds a key, a <code>keydown </code>event is triggered once, but separate <code>keypress</code> events are triggered for each inserted character. In addition, modifier keys (such as Shift) trigger <code>keydown</code> events but not <code>keypress</code> events.</p>
<p>A <code>keypress</code> event handler can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.</p>
<p>For example, consider the HTML:</p>
<pre><code>&lt;form&gt;
&lt;fieldset&gt;
&lt;input id="target" type="text" value="Hello there" /&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;div id="other"&gt;
Trigger the handler
&lt;/div&gt;</code></pre>
<p>The event handler can be bound to the input field:</p>
<pre><code>$("#target").keypress(function() {
alert("Handler for .keypress() called.");
});</code></pre>
<p>Now when the insertion point is inside the field, pressing a key displays the alert:</p>
<p>
<span class="output">Handler for .keypress() called.</span>
</p>
<p>The message repeats if the key is held down. To trigger the event manually, apply <code>.keypress()</code> without an argument::</p>
<pre><code>$('#other').click(function() {
$("#target").keypress();
});</code></pre>
<p>After this code executes, clicks on <span class="output">Trigger the handler</span> will also alert the message.</p>
<p>If key presses anywhere need to be caught (for example, to implement global shortcut keys on a page), it is useful to attach this behavior to the <code>document</code> object. Because of event bubbling, all key presses will make their way up the DOM to the <code>document</code> object unless explicitly stopped.</p>
<p>To determine which character was entered, examine the <code>event</code> object that is passed to the handler function. While browsers use differing properties to store this information, jQuery normalizes the <code>.which</code> property so you can reliably use it to retrieve the character code.</p>
<p>Note that <code>keydown</code> and <code>keyup</code> provide a code indicating which key is pressed, while <code>keypress</code> indicates which character was entered. For example, a lowercase "a" will be reported as 65 by <code>keydown</code> and <code>keyup</code>, but as 97 by <code>keypress</code>. An uppercase "A" is reported as 65 by all events. Because of this distinction, when catching special keystrokes such as arrow keys, <code>.keydown()</code> or <code>.keyup()</code> is a better choice.</p>
</longdesc>
<example>
<desc>Show the event object when a key is pressed in the input. Note: This demo relies on a simple $.print() plugin (http://api.jquery.com/scripts/events.js) for the event object's output.</desc>
<code><![CDATA[
var xTriggered = 0;
$("#target").keypress(function(event) {
if ( event.which == 13 ) {
event.preventDefault();
}
xTriggered++;
var msg = "Handler for .keypress() called " + xTriggered + " time(s).";
$.print( msg, "html" );
$.print( event );
});
$("#other").click(function() {
$("#target").keypress();
});]]></code>
<css><![CDATA[
fieldset { margin-bottom: 1em; }
input { display: block; margin-bottom: .25em; }
#print-output {
width: 100%;
}
.print-output-line {
white-space: pre;
padding: 5px;
font-family: monaco, monospace;
font-size: .7em;
}
]]></css>
<height>460</height>
<html><![CDATA[<form>
<fieldset>
<label for="target">Type Something:</label>
<input id="target" type="text" />
</fieldset>
</form>
<button id="other">
Trigger the handler
</button>
<script src="http://api.jquery.com/scripts/events.js"></script>
]]></html>
</example>
<category slug="events/keyboard-events"/>
<category slug="version/1.0"/>
<category slug="version/1.4.3"/>
</entry>
Jump to Line
Something went wrong with that request. Please try again.