HTMLWindowEvents4::onkeypress Event

New for Internet Explorer 9

[This documentation is preliminary and is subject to change.]

Fires when the user presses an alphanumeric key.


VARIANT_BOOL onkeypress(
    IHTMLEventObj *pEvtObj


[in] Pointer to an IHTMLEventObj interface for the current event. This is the same interface returned from IHTMLWindow2::event.

Return Value

If the event bubbles and is cancellable, return VARIANT_TRUE to prevent the event from bubbling to other event handlers in the document tree. Return VARIANT_FALSE to allow bubbling.



The DISPID for this event is defined in mshtmdid.h. Use this value to identify the event handler when implementing IDispatch::Invoke.

Event Information

Bubbles Yes
Cancels Yes
To invoke Press any alphanumeric keyboard key.
Default action Returns a number specifying the Unicode value of the key that was pressed.

Event Object Properties

The IHTMLEventObj interface contains additional information about the current event. When the onkeypress event is fired, you can use the IHTMLWindow2::event property to retrieve an IHTMLEventObj interface. To obtain an alternate interface pointer such as IHTMLEventObj2 or IHTMLEventObj3, call QueryInterface on the object.

Available Properties

IDOMKeyboardEvent::key New for Internet Explorer 9  Gets the key identifier of a key that is pressed.
IHTMLEventObj::keyCode Sets or retrieves the Unicode key code associated with the key that caused the event.
IDOMKeyboardEvent::location New for Internet Explorer 9  Gets the location of a key on the keyboard or device.
IDOMKeyboardEvent::repeat New for Internet Explorer 9  Gets a value that indicates whether a key was pressed and held.

Refer to the specific event object for additional event properties.


As of Microsoft Internet Explorer 4.0, the HTMLWindowEvents4::onkeypress event fires and can be canceled for the following keys:

  • Letters: A - Z (uppercase and lowercase)
  • Numerals: 0 - 9
  • Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~

See Also

Community Additions