* Removing the parseXML functionality. Will be moved into More or into a separate...
[mootools/dkf.git] / Docs / Types / Event.md
blobafd6e64f5d1614a1804e413327803fd49ffb0a2c
1 Type: Event {#Event}
2 ====================
4 MooTools Event Methods.
6 Event Method: constructor {#Event:constructor}
7 ----------------------------------------------
9 ### Syntax:
11         new Event([event[, win]]);
13 ### Arguments:
15 1. event - (*event*, required) An HTMLEvent Object.
16 2. win   - (*window*, optional: defaults to window) The context of the event.
18 #### Properties:
20 * page.x        - (*number*) The x position of the mouse, relative to the full window.
21 * page.y        - (*number*) The y position of the mouse, relative to the full window.
22 * client.x      - (*number*) The x position of the mouse, relative to the viewport.
23 * client.y      - (*number*) The y position of the mouse, relative to the viewport.
24 * rightClick    - (*boolean*) True if the user clicked the right mousebutton
25 * wheel         - (*number*) The amount of third button scrolling.
26 * relatedTarget - (*element*) The event related target, NOT `extended` with [$][].
27 * target        - (*element*) The event target, not extended with [$][] for performance reasons.
28 * code          - (*number*) The keycode of the key pressed.
29 * key           - (*string*) The key pressed as a lowercase string. key can be 'enter', 'up', 'down', 'left', 'right', 'space', 'backspace', 'delete', and 'esc'.
30 * shift         - (*boolean*) True if the user pressed the shift key.
31 * control       - (*boolean*) True if the user pressed the control key.
32 * alt           - (*boolean*) True if the user pressed the alt key.
33 * meta          - (*boolean*) True if the user pressed the meta key.
35 ### Examples:
37         $('myLink').addEvent('keydown', function(event){
38                 // the passed event parameter is already an instance of the Event class.
39                 alert(event.key);   // returns the lowercase letter pressed.
40                 alert(event.shift); // returns true if the key pressed is shift.
41                 if (event.key == 's' && event.control) alert('Document saved.'); //executes if the user presses Ctr+S.
42         });
44 ### Notes:
46 - Accessing event.page / event.client requires the page to be in [Standards Mode](http://hsivonen.iki.fi/doctype/).
47 - Every event added with addEvent gets the MooTools method automatically, without the need to manually instance it.
50 Event Method: stop {#Event:stop}
51 --------------------------------
53 Stop an Event from propagating and also executes preventDefault.
55 ### Syntax:
57         myEvent.stop();
59 ### Returns:
61 * (*object*) This Event instance.
63 ### Examples:
65 ##### HTML:
67         <a id="myAnchor" href="http://google.com/">Visit Google.com</a>
69 ##### JavaScript
71         $('myAnchor').addEvent('click', function(event){
72                 event.stop(); //Prevents the browser from following the link.
73                 this.set('text', 'Where do you think you\'re going?'); //'this' is Element that fires the Event.
74                 (function(){
75                         this.set('text','Instead visit the Blog.').set('href', 'http://blog.mootools.net');
76                 }).delay(500, this);
77         });
79 ### Notes:
81 - Returning false within the function can also stop the propagation of the Event.
83 ### See Also:
85 - [Element.addEvent](#Element:addEvent), [Element.stopPropagation](#Event:stopPropagation), [Event.preventDefault](#Event:preventDefault), [Function:delay][]
89 Event Method: stopPropagation {#Event:stopPropagation}
90 ------------------------------------------------------
92 Cross browser method to stop the propagation of an event (this stops the event from bubbling up through the DOM).
94 ### Syntax:
96         myEvent.stopPropagation();
98 ### Returns:
100 * (*object*) This Event object.
102 ### Examples:
104 "#myChild" does not cover the same area as myElement. Therefore, the 'click' differs from parent and child depending on the click location:
106 ##### HTML:
108         <div id="myElement">
109                 <div id="myChild"></div>
110         </div>
112 ##### JavaScript
114         $('myElement').addEvent('click', function(){
115                 alert('click');
116                 return false; //equivalent to stopPropagation.
117         });
118         $('myChild').addEvent('click', function(event){
119                 event.stopPropagation(); //prevents the event from bubbling up, and fires the parent's click event.
120         });
122 ### See Also:
124 - [Element:addEvent][]
125 - [MDC event.stopPropagation](http://developer.mozilla.org/en/docs/DOM:event.stopPropagation)
129 Event Method: preventDefault {#Event:preventDefault}
130 ----------------------------------------------------
132 Cross browser method to prevent the default action of the event.
134 ### Syntax:
136         myEvent.preventDefault();
138 ### Returns:
140 * (*object*) This Event object.
142 ### Examples:
144 ##### HTML:
146         <form>
147                 <input id="myCheckbox" type="checkbox" />
148         </form>
150 ##### JavaScript
152         $('myCheckbox').addEvent('click', function(event){
153                 event.preventDefault(); //prevents the checkbox from being "checked".
154         });
156 ### See Also:
158 - [Element:addEvent][]
159 - [MDC event.preventDefault](http://developer.mozilla.org/en/docs/DOM:event.preventDefault)
162 Object: Event.Keys {#Event-Keys}
163 ==============================
165 Additional Event key codes can be added by adding properties to the Event.Keys Object.
167 #### Example:
169     Event.Keys.shift = 16;
170     $('myInput').addEvent('keydown', function(event){
171             if (event.key == 'shift') alert('You pressed shift.');
172     });
174 #### Possible Keys:
176 - enter
177 - up
178 - down
179 - left
180 - right
181 - esc
182 - space
183 - backspace
184 - tab
185 - delete
187 ### See Also:
189 - [MooTools More Keyboard][]
191 ### Note:
193 Since MooTools 1.3 this is a native JavaScript Object and not an instance of the deprecated Hash
196 [Element:addEvent]: /core/Element/Element.Event#Element:addEvent
197 [Function]: /core/Types/Function
198 [Function:bind]: /core/Types/Function/#Function:bind
199 [Function:pass]: /core/Types/Function/#Function:pass
200 [Function:delay]: /core/Types/Function/#Function:delay
201 [MooTools More Keyboard]: /more/Interface/Keyboard