onkeydown Event

Internet Development Index

Fires when the user presses a key.

Syntax

Inline HTML<ELEMENT onkeydown = "handler" ... > All platforms
Event propertyobject.onkeydown = handlerJScript only
object.onkeydown = GetRef("handler")Visual Basic Scripting Edition (VBScript) 5.0 or later only
Named script <SCRIPT FOR = object EVENT = onkeydown> Internet Explorer only

Event Information

BubblesYes
CancelsYes
To invoke Press any keyboard key.
Default action Returns a number specifying the keyCode of the key that was pressed.

Event Object Properties

Although event handlers in the DHTML Object Model do not receive parameters directly, a handler can query an event object for data.

Available Properties

altKey Sets or retrieves a value that indicates the state of the ALT key.
altLeft Sets or retrieves a value that indicates the state of the left ALT key.
cancelBubble Sets or retrieves whether the current event should bubble up the hierarchy of event handlers.
clientX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.
clientY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.
ctrlKey Sets or retrieves the state of the CTRL key.
ctrlLeft Sets or retrieves the state of the left CTRL key.
keyCode Sets or retrieves the Unicode key code associated with the key that caused the event.
offsetX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the object firing the event.
offsetY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the object firing the event.
repeat Retrieves whether the onkeydown event is being repeated.
returnValue Sets or retrieves the return value from the event.
screenX Retrieves the x-coordinate of the mouse pointer's position relative to the user's screen.
screenY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the user's screen.
shiftKey Sets or retrieves the state of the SHIFT key.
shiftLeft Retrieves the state of the left SHIFT key.
srcElement Sets or retrieves the object that fired the event.
type Sets or retrieves the event name from the event object.
x Sets or retrieves the x-coordinate, in pixels, of the mouse pointer's position relative to a relatively positioned parent element.
y Sets or retrieves the y-coordinate, in pixels, of the mouse pointer's position relative to a relatively positioned parent element.

Remarks

As of Microsoft® Internet Explorer 4.0, the onkeydown event fires for the following keys:

  • Editing: DELETE, INSERT
  • Function: F1 - F12
  • Letters: A - Z (uppercase and lowercase)
  • Navigation: HOME, END, LEFT ARROW, RIGHT ARROW, UP ARROW, DOWN ARROW
  • Numerals: 0 - 9
  • Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~
  • System: ESC, SPACEBAR, SHIFT, TAB

As of Internet Explorer 5, the event also fires for the following keys:

  • Editing: BACKSPACE
  • Navigation: PAGE UP, PAGE DOWN
  • System: SHIFT+TAB

As of Internet Explorer 5, this event can be canceled for the following keys and key combinations by specifying event.returnValue=false:

  • Editing: BACKSPACE, DELETE
  • Letters: A - Z (uppercase and lowercase)
  • Navigation: PAGE UP, PAGE DOWN, END, HOME, LEFT ARROW, RIGHT ARROW, UP ARROW, DOWN ARROW
  • Numerals: 0 - 9
  • Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~
  • System: SPACEBAR, ESC, TAB, SHIFT+TAB

You can cancel all keys that fire the onkeydown event in HTML Applications, including most accelerator keys, such as ALT+F4.

In Internet Explorer 4.0, you cannot cancel the onkeydown event, but you can use the onkeypress event to cancel keyboard events.

Example

This example uses the onkeydown event to cancel input from the keyboard.

<SCRIPT>
function fnTrapKD(){
if(oTrap.checked){
oOutput.innerText+="[trap = " + event.keyCode + "]";
event.returnValue=false;
}
else{
oOutput.innerText+=String.fromCharCode(event.keyCode);
}
}
</SCRIPT>
<INPUT TYPE="checkbox" ID="oTrap">
<INPUT ID="oExample" TYPE="text" onkeydown="fnTrapKD()">
<TEXTAREA ID="oOutput" ROWS="10" COLS="50">
</TEXTAREA>
This feature requires Microsoft® Internet Explorer 5 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Standards Information

This event is defined in HTML 4.0 Non-Microsoft link.

Applies To

A, ACRONYM, ADDRESS, APPLET, AREA, B, BDO, BIG, BLOCKQUOTE, BODY, BUTTON, CAPTION, CENTER, CITE, CODE, CUSTOM, DD, DEL, DFN, DIR, DIV, document, DT, EM, FIELDSET, FONT, FORM, hn, HR, I, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, KBD, LABEL, LEGEND, LI, LISTING, MAP, MARQUEE, MENU, nextID, NOBR, OBJECT, OL, P, PLAINTEXT, PRE, Q, RT, RUBY, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, XMP

See Also

onkeyup