Availability: |
| |||||||
Property/method value type: | Boolean primitive | |||||||
JavaScript syntax: | - | myObject.onkeypress = aHandler | ||||||
HTML syntax: | <A onKeyPress="..."> <AREA onKeyPress="..."> <BODY onKeyPress="..."> <HTMLTag onKeyPress="..."> <IMG onKeyPress="..."> <INPUT onKeyPress="..."> | |||||||
Argument list: | aHandler | A reference to a function object to handle the event |
This handler is sometimes more appropriate than the onKeyDown and onKeyUp handlers. They should be used if you want some action to occur on each transition and to indicate by some feedback mechanism to the user that the key is still down.
As the key is pressed down and then released again, this event is triggered. If you return the value true, the event will be passed to the browser for further processing. Returning false inhibits this action and discards the message.
In Netscape, the key code of the key that triggered the event is available in the which property of the Event object that is passed to the event handler as an argument. The equivalent value in MSIE is available in the keyCode property of the event object referenced by the window.event property.
The key value is the Unicode character code point value, which is a numeric value. This may need to be converted to a character if you want to display it. You can do that with the String.fromCharCode() static method, which will create a one character string for you.
You may need to determine whether any of the modifier keys were pressed. In Netscape, you retrieve a bitmask value from the modifiers property of the Event object. This can then be masked against the various bit flags available as static properties of the Event class.
In MSIE, modifier keys are available with a set of properties that return the state of each modifier key individually.
In MSIE, you can modify the value of the key that is passed back to the browser by storing a different Unicode code point value in the returnValue property of the Event object.
This event was only supported by TextCell objects in the WebTV platform from the Summer 2000 release onwards.
An onKeyPress event will also trigger an onKeyDown event and an onKeyUp event. The onKeyPress will not be triggered until the onKeyUp occurs. There is no guarantee that the onKeyUp will happen before the onKeyPress although that is the logical sequence.
You do not receive a message when a modifier key is held down on its own. However you could sense the state of that as keys are processed. It isn't ideal but may be sufficient.
A
ACRONYM
ADDRESS
Anchor
APPLET
AREA
B
BIG
BLOCKQUOTE
BODY
BUTTON
CAPTION
CENTER
Checkbox
CITE
CODE
DD
DEL
DFN
DIR
DIV
DL
Document
DT
EM
FIELDSET
FileUpload
FONT
FORM
H1
HR
I
IMG
Input
INS
KBD
LABEL
LEGEND
LI
LISTING
MAP
MARQUEE
MENU
OL
P
Password
PLAINTEXT
PRE
Q
RadioButton
ResetButton
S
SAMP
Select
SMALL
SPAN
STRIKE
STRONG
SUB
SubmitButton
SUP
TABLE
TBODY
TD
TEXTAREA
TextCell
TFOOT
TH
THEAD
TR
TT
U
UL
Url
VAR
Prev | Home | Next |
onKeyDown | Up | onKeyUp |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |