JavaScript Kit > JavaScript Reference > Here
Keyboard and Mouse buttons events
The Event object keeps track and lets you react to keyboard and mouse events, such as when a particular keyboard or mouse button is pressed. This is realized through 4 event handlers and a few Event object properties.
Related Tutorials
Keyboard/ Mouse event handlers
Event Handler | Description |
---|---|
onkeypress | Fires when the user presses a key on the associated
element (ie: document, DIV etc). A combination of onkeydown
and onkeyup . |
onkeydown | Fires when the user depresses a key (but not yet released) on the associated element. |
onkeyup | Fires when the user releases a key after having depressed it on the associated element. |
onclick | Fires when the user clicks on an element. A
combination of onmousedown and onmouseup . |
onmousedown | FIres when the user holds the mouse down over an element. |
onmouseup | Fires when the user releases the mouse after having hold it down over the element. |
ondblclick | Fires when the user double clicks the mouse over an element. |
Keyboard Event object properties
Below lists the relevant properties within the
Event Object that
cater to keyboard actions. Recall that in IE, the event object is accessed
directly via window.event
, while in Firefox and other browsers, it
is indirectly passed as the first parameter the callback function associated
with this event.
Properties | Description |
---|---|
altKey, ctrlKey, shiftKey | Boolean properties that indicate whether the Alt, Ctrl, or Shift keys were pressed at time of the event. |
charCode | Returns the character code of the key pressed during
an onkeypress event. and is only set
for keys in which a character is associated with it (ie: "a",
"b", or "z"). Keys with no character association like "Shift"
or "Ctrl" do not qualify. Other points:
Example: document.onkeypress=function(e){ |
isChar | Boolean property that indicates if the key pressed has a character associated with it. If the user pressed "F1" for example, the property returns false. Buggy as of Firefox 2.x, not supported in most other browsers (ie: IE, Opera 9 etc). |
keyCode | Property indicating the key code pressed during an onkeydown and
onkeyup event, and in IE, onkeypress
as well. The keyCode property is set whenever a key is pressed,
including non character keys like "Shift" or "Ctrl".
Be careful when accessing the Compare this example (try pressing "a" or "Shift a" in the "char" field): <form> to this example (try pressing "a" or "Shift a" in the "char" field): <form> |
metaKey | Boolean property indicating whether the META key was pressed. NOT supported in IE. |
returnValue | Set to false to cancel any default action for the event. |
which | Returns the unicode value of the key pressed whether
it has a character associated with it or not. Behaves similar to IE's
version of the
keyCode property and supported in Firefox/ Opera.
NOT supported in IE. |
The below table lists the Unicode character code value of the primary keys on your keyboard:
Mouse Event object properties
Below lists the relevant properties within the Event Object that cater to which mouse button(s) was pressed.
Properties | Description |
---|---|
button | An integer indicating which mouse button was
pressed:
You should not count on this order across browsers and different mouse devices. In IE6 and lower for example, 1 is used to indicate left button instead, for example. |
Example- Live validation of a text field to only allow alphabetical (upper and lower case), SPACE and the BACKSPACE keys.
The following example uses the Event Object and keyboard related properties to only allow alphabetical keys plus BACKSPACE and SPACE in a form field.
<form>
<input type="text" id="alphanumeric" size="25"></textarea>
</form>
<script type="text/javascript">
document.getElementById("alphanumeric").onkeypress=function(e){
var e=window.event || e
var keyunicode=e.charCode || e.keyCode
//Allow alphabetical keys, plus BACKSPACE and SPACE
return (keyunicode>=65 && keyunicode<=122 || keyunicode==8 || keyunicode==32)?
true : false
}
</script>
Demo:
- JavaScript Operators
- JavaScript Statements
- Global functions
- JavaScript Events
- Escape Sequences
- Reserved Words