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.
The below table lists the Unicode character code value of the primary keys on your keyboard:
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 viawindow.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:
document.onkeypress=function(e){ var e=window.event || e alert("CharCode value: "+e.charCode) alert("Character: "+String.fromCharCode(e.charCode)) } |
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".
keyCode property during an onkeydown or onkeyup event, as it is set whenever any key is pressed, including non character keys like "Shift". This means if you try to press "Shift+a" to try and get the keyCode for "A", you will always end up getting two keyCodes instead, one for "Shift" and one for "A" in that order. What you won't get regardless is the keyCode for "a", as keyCode always returns the unicode value of the uppercase version of a character. To derive the keyCode for "a" (lowercase), you must probe the keyCode returned during the onkeypress event in IE, and since Firefox doesn't set keyCode during onkeypress , switch to e.charCode or e.which instead for that browser. Sounds confusing? Yes it is. Check out the below two examples for a more hands on explanation: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):Char: <input type="text" id="char" size="15" /> Keycode: <input type="text" id="keycode" size="15" /> </form> <script type="text/javascript"> var charfield=document.getElementById("char") charfield.onkeydown=function(e){ var e=window.event || e document.getElementById("keycode").value=e.keyCode } </script> <form> Char: <input type="text" id="char" size="15" /> Keycode: <input type="text" id="unicode" size="15" /> </form> <script type="text/javascript"> var charfield=document.getElementById("char") charfield.onkeypress=function(e){ var e=window.event || e var keyunicode=e.charCode || e.keyCode document.getElementById("unicode").value=keyunicode } </script> |
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. |
- a97
- A65
- b98
- B66
- c99
- C67
- d100
- D68
- e101
- E69
- f102
- F70
- g103
- G71
- h104
- H72
- i105
- I73
- j106
- J74
- k107
- K75
- l108
- L76
- m109
- M77
- n110
- N78
- o111
- O79
- p112
- P80
- q113
- Q81
- r114
- R82
- s115
- S83
- t116
- T84
- u117
- U85
- v118
- V86
- w119
- W87
- x120
- X88
- y121
- Y89
- z122
- Z90
- 048
- 149
- 250
- 351
- 452
- 553
- 654
- 755
- 856
- 957
- F1112
- F2113
- F3114
- F4115
- F5116
- F6117
- F7118
- F8119
- F9120
- F10121
- F11122
- F12123
- Space32
- BackSpace8
- Enter13
- Shift16
- Ctrl17
- Alt18
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:
|
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:<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>