Event Object
When you connect a function to a DOM event with dojo.connect, dojo passes your function a normalized event object. This means that, regardless of the client's browser, you can count on a set of standard attributes about the event and a set of methods to manipulate the event.
Syntax
Assume that your function has been called by dojo.connect and takes an argument named event
Dojo provides the following attributes with an event object:
- event.target — the element that generated the event
- event.currentTarget — the current target
- event.layerX — the x coordinate, relative to the event.currentTarget
- event.layerY — the y coordinate, relative to the event.currentTarget
- event.pageX — the x coordinate, relative to the view port
- event.pageY — the y coordinate, relative to the view port
- event.relatedTarget — For onmouseover and onmouseout, the object that the mouse pointer is moving to or out of
- event.charCode — For keypress events, the character code of the key pressed
Dojo provides the following methods with an event object:
- event.preventDefault — prevent an event's default behavior (e.g., a link from loading a new page)
- event.stopPropagation — prevent an event from triggering a parent node's event
Additionally, dojo.stopEvent(event) will prevent both default behavior any any propagation (bubbling) of an event.
Example Code for Reference
/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */
.geshifilter {font-family: monospace;}
.geshifilter .imp {font-weight: bold; color: red;}
.geshifilter .kw1 {color: #b1b100;}
.geshifilter .kw2 {color: #000000; font-weight: bold;}
.geshifilter .kw3 {color: #000066;}
.geshifilter .coMULTI {color: #808080; font-style: italic;}
.geshifilter .es0 {color: #000099; font-weight: bold;}
.geshifilter .br0 {color: #66cc66;}
.geshifilter .st0 {color: #ff0000;}
.geshifilter .nu0 {color: #cc66cc;}
.geshifilter .sc0 {color: #00bbdd;}
.geshifilter .sc1 {color: #ddbb00;}
.geshifilter .sc2 {color: #009900;}
<head>
<title>Dojo Events are Great
</title>
<script type="text/javascript" src="http://o.aolcdn.com/dojo/1.0.0/dojo/dojo.xd.js"></script>
<script type="text/javascript">
function echo(event) {
key = event.charCode;
console.debug(event.charCode);
}
function foo(event) {
dojo.stopEvent(event);
console.debug("The link was clicked");
}
dojo.addOnLoad(function() {
interactiveNode = dojo.byId("interactive");
linkNode = dojo.byId("link");
dojo.connect(interactiveNode, 'onkeypress', echo);
dojo.connect(linkNode, 'onclick', foo);
});
</script>
<body>
<a href="http://dojotoolkit.org" id="link">Dojo
</a> is great.
<form>
<label for="infield"> Type some text:
</label>
<input id="interactive" type="text" name="infield">
</form>
</body>
Using a Dojo Event Object
In the example code, we have two functions that are connected to two different events. Echo sends the key code of any key typed in the text input field to the console. It does so by using the charCode property of the normalized event object. Foo is connected to the #link and cause it to send "The link was clicked" to the console instead of changing the browser's location; by using the preventDefault method of the normalized event object, connections to change the default behavior of DOM objects.
Now, imagine that you want to detect for the down arrow key in the text box. To do this, we just need to attach a new event listener to the text box and check to see if each keycode is the keycode for the down arrow. And how do you know what the keycode for the down arrow is, you may ask? Well, Dojo provides constants for every non-alpha-numeric key (see: Key code constants). In our case, we are interested in dojo.keys.DOWN_ARROW. So, assuming that you want to just log when the down arrow is pressed, the following code should do the job:
/* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter) */
.geshifilter {font-family: monospace;}
.geshifilter .imp {font-weight: bold; color: red;}
.geshifilter .kw1 {color: #000066; font-weight: bold;}
.geshifilter .kw2 {color: #003366; font-weight: bold;}
.geshifilter .kw3 {color: #000066;}
.geshifilter .co1 {color: #009900; font-style: italic;}
.geshifilter .coMULTI {color: #009900; font-style: italic;}
.geshifilter .es0 {color: #000099; font-weight: bold;}
.geshifilter .br0 {color: #66cc66;}
.geshifilter .st0 {color: #3366CC;}
.geshifilter .nu0 {color: #CC0000;}
.geshifilter .me1 {color: #006600;}
.geshifilter .re0 {color: #0066FF;}
dojo.connect(interactiveNode, 'onkeypress', function (evt) {
key = evt.keyCode;
if(key == dojo.keys.DOWN_ARROW) {
console.debug("The user pressed the down arrow!");
}
});
Key Code Constants
Dojo provides the following keycode constants in the namespace dojo.keys:
- BACKSPACE
- TAB
- CLEAR
- ENTER
- SHIFT
- CTRL
- ALT
- PAUSE
- CAPS_LOCK
- ESCAPE
- SPACE
- PAGE_UP
- PAGE_DOWN
- END
- HOME
- LEFT_ARROW
- UP_ARROW
- RIGHT_ARROW
- DOWN_ARROW
- INSERT
- DELETE
- HELP
- LEFT_WINDOW
- RIGHT_WINDOW
- SELECT
- NUMPAD_0
- NUMPAD_1
- NUMPAD_2
- NUMPAD_3
- NUMPAD_4
- NUMPAD_5
- NUMPAD_6
- NUMPAD_7
- NUMPAD_8
- NUMPAD_9
- NUMPAD_MULTIPLY
- NUMPAD_PLUS
- NUMPAD_ENTER
- NUMPAD_MINUS
- NUMPAD_PERIOD
- NUMPAD_DIVIDE
- F1
- F2
- F3
- F4
- F5
- F6
- F7
- F8
- F9
- F10
- F11
- F12
- F13
- F14
- F15
- NUM_LOCK
- SCROLL_LOCK