OLD | NEW |
1 /** | 1 /** |
2 * A custom KeyboardEvent that attempts to eliminate cross-browser | 2 * A custom KeyboardEvent that attempts to eliminate cross-browser |
3 * inconsistencies, and also provide both keyCode and charCode information | 3 * inconsistencies, and also provide both keyCode and charCode information |
4 * for all key events (when such information can be determined). | 4 * for all key events (when such information can be determined). |
5 * | 5 * |
6 * KeyEvent tries to provide a higher level, more polished keyboard event | 6 * KeyEvent tries to provide a higher level, more polished keyboard event |
7 * information on top of the "raw" [KeyboardEvent]. | 7 * information on top of the "raw" [KeyboardEvent]. |
8 * | 8 * |
9 * The mechanics of using KeyEvents is a little different from the underlying | 9 * The mechanics of using KeyEvents is a little different from the underlying |
10 * [KeyboardEvent]. To use KeyEvents, you need to create a stream and then add | 10 * [KeyboardEvent]. To use KeyEvents, you need to create a stream and then add |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 _currentTarget = _parent.currentTarget; | 86 _currentTarget = _parent.currentTarget; |
87 } | 87 } |
88 | 88 |
89 /** Programmatically create a new KeyEvent (and KeyboardEvent). */ | 89 /** Programmatically create a new KeyEvent (and KeyboardEvent). */ |
90 factory KeyEvent(String type, | 90 factory KeyEvent(String type, |
91 {Window view, | 91 {Window view, |
92 bool canBubble: true, | 92 bool canBubble: true, |
93 bool cancelable: true, | 93 bool cancelable: true, |
94 int keyCode: 0, | 94 int keyCode: 0, |
95 int charCode: 0, | 95 int charCode: 0, |
96 int keyLocation: 1, | 96 int location: 1, |
97 bool ctrlKey: false, | 97 bool ctrlKey: false, |
98 bool altKey: false, | 98 bool altKey: false, |
99 bool shiftKey: false, | 99 bool shiftKey: false, |
100 bool metaKey: false, | 100 bool metaKey: false, |
101 EventTarget currentTarget}) { | 101 EventTarget currentTarget}) { |
102 if (view == null) { | 102 if (view == null) { |
103 view = window; | 103 view = window; |
104 } | 104 } |
105 | 105 |
106 var eventObj; | 106 var eventObj; |
107 // In these two branches we create an underlying native KeyboardEvent, but | 107 // In these two branches we create an underlying native KeyboardEvent, but |
108 // we set it with our specified values. Because we are doing custom setting | 108 // we set it with our specified values. Because we are doing custom setting |
109 // of certain values (charCode/keyCode, etc) only in this class (as opposed | 109 // of certain values (charCode/keyCode, etc) only in this class (as opposed |
110 // to KeyboardEvent) and the way we set these custom values depends on the | 110 // to KeyboardEvent) and the way we set these custom values depends on the |
111 // type of underlying JS object, we do all the construction for the | 111 // type of underlying JS object, we do all the construction for the |
112 // underlying KeyboardEvent here. | 112 // underlying KeyboardEvent here. |
113 if (canUseDispatchEvent) { | 113 if (canUseDispatchEvent) { |
114 // Currently works in everything but Internet Explorer. | 114 // Currently works in everything but Internet Explorer. |
115 eventObj = new Event.eventType('Event', type, | 115 eventObj = new Event.eventType('Event', type, |
116 canBubble: canBubble, cancelable: cancelable); | 116 canBubble: canBubble, cancelable: cancelable); |
117 | 117 |
118 JS('void', '#.keyCode = #', eventObj, keyCode); | 118 JS('void', '#.keyCode = #', eventObj, keyCode); |
119 JS('void', '#.which = #', eventObj, keyCode); | 119 JS('void', '#.which = #', eventObj, keyCode); |
120 JS('void', '#.charCode = #', eventObj, charCode); | 120 JS('void', '#.charCode = #', eventObj, charCode); |
121 | 121 |
122 JS('void', '#.keyLocation = #', eventObj, keyLocation); | 122 JS('void', '#.location = #', eventObj, location); |
123 JS('void', '#.ctrlKey = #', eventObj, ctrlKey); | 123 JS('void', '#.ctrlKey = #', eventObj, ctrlKey); |
124 JS('void', '#.altKey = #', eventObj, altKey); | 124 JS('void', '#.altKey = #', eventObj, altKey); |
125 JS('void', '#.shiftKey = #', eventObj, shiftKey); | 125 JS('void', '#.shiftKey = #', eventObj, shiftKey); |
126 JS('void', '#.metaKey = #', eventObj, metaKey); | 126 JS('void', '#.metaKey = #', eventObj, metaKey); |
127 } else { | 127 } else { |
128 // Currently this works on everything but Safari. Safari throws an | 128 // Currently this works on everything but Safari. Safari throws an |
129 // "Attempting to change access mechanism for an unconfigurable property" | 129 // "Attempting to change access mechanism for an unconfigurable property" |
130 // TypeError when trying to do the Object.defineProperty hack, so we avoid | 130 // TypeError when trying to do the Object.defineProperty hack, so we avoid |
131 // this branch if possible. | 131 // this branch if possible. |
132 // Also, if we want this branch to work in FF, we also need to modify | 132 // Also, if we want this branch to work in FF, we also need to modify |
(...skipping 15 matching lines...) Expand all Loading... |
148 " get : function() { return this.keyCodeVal; } })", | 148 " get : function() { return this.keyCodeVal; } })", |
149 eventObj); | 149 eventObj); |
150 JS( | 150 JS( |
151 'void', | 151 'void', |
152 "Object.defineProperty(#, 'charCode', {" | 152 "Object.defineProperty(#, 'charCode', {" |
153 " get : function() { return this.charCodeVal; } })", | 153 " get : function() { return this.charCodeVal; } })", |
154 eventObj); | 154 eventObj); |
155 | 155 |
156 var keyIdentifier = _convertToHexString(charCode, keyCode); | 156 var keyIdentifier = _convertToHexString(charCode, keyCode); |
157 eventObj._initKeyboardEvent(type, canBubble, cancelable, view, | 157 eventObj._initKeyboardEvent(type, canBubble, cancelable, view, |
158 keyIdentifier, keyLocation, ctrlKey, altKey, shiftKey, metaKey); | 158 keyIdentifier, location, ctrlKey, altKey, shiftKey, metaKey); |
159 JS('void', '#.keyCodeVal = #', eventObj, keyCode); | 159 JS('void', '#.keyCodeVal = #', eventObj, keyCode); |
160 JS('void', '#.charCodeVal = #', eventObj, charCode); | 160 JS('void', '#.charCodeVal = #', eventObj, charCode); |
161 } | 161 } |
162 // Tell dart2js that it smells like a KeyboardEvent! | 162 // Tell dart2js that it smells like a KeyboardEvent! |
163 setDispatchProperty(eventObj, _keyboardEventDispatchRecord); | 163 setDispatchProperty(eventObj, _keyboardEventDispatchRecord); |
164 | 164 |
165 var keyEvent = new KeyEvent.wrap(eventObj); | 165 var keyEvent = new KeyEvent.wrap(eventObj); |
166 if (keyEvent._currentTarget == null) { | 166 if (keyEvent._currentTarget == null) { |
167 keyEvent._currentTarget = currentTarget == null ? window : currentTarget; | 167 keyEvent._currentTarget = currentTarget == null ? window : currentTarget; |
168 } | 168 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 String get code => _parent.code; | 206 String get code => _parent.code; |
207 /** True if the ctrl key is pressed during this event. */ | 207 /** True if the ctrl key is pressed during this event. */ |
208 bool get ctrlKey => _parent.ctrlKey; | 208 bool get ctrlKey => _parent.ctrlKey; |
209 int get detail => _parent.detail; | 209 int get detail => _parent.detail; |
210 String get key => _parent.key; | 210 String get key => _parent.key; |
211 /** | 211 /** |
212 * Accessor to the part of the keyboard that the key was pressed from (one of | 212 * Accessor to the part of the keyboard that the key was pressed from (one of |
213 * KeyLocation.STANDARD, KeyLocation.RIGHT, KeyLocation.LEFT, | 213 * KeyLocation.STANDARD, KeyLocation.RIGHT, KeyLocation.LEFT, |
214 * KeyLocation.NUMPAD, KeyLocation.MOBILE, KeyLocation.JOYSTICK). | 214 * KeyLocation.NUMPAD, KeyLocation.MOBILE, KeyLocation.JOYSTICK). |
215 */ | 215 */ |
216 int get keyLocation => _parent.keyLocation; | 216 int get location => _parent.location; |
217 /** True if the Meta (or Mac command) key is pressed during this event. */ | 217 /** True if the Meta (or Mac command) key is pressed during this event. */ |
218 bool get metaKey => _parent.metaKey; | 218 bool get metaKey => _parent.metaKey; |
219 /** True if the shift key was pressed during this event. */ | 219 /** True if the shift key was pressed during this event. */ |
220 bool get shiftKey => _parent.shiftKey; | 220 bool get shiftKey => _parent.shiftKey; |
221 Window get view => _parent.view; | 221 Window get view => _parent.view; |
222 void _initUIEvent( | 222 void _initUIEvent( |
223 String type, bool canBubble, bool cancelable, Window view, int detail) { | 223 String type, bool canBubble, bool cancelable, Window view, int detail) { |
224 throw new UnsupportedError("Cannot initialize a UI Event from a KeyEvent."); | 224 throw new UnsupportedError("Cannot initialize a UI Event from a KeyEvent."); |
225 } | 225 } |
226 | 226 |
227 String get _shadowKeyIdentifier => JS('String', '#.keyIdentifier', _parent); | 227 String get _shadowKeyIdentifier => JS('String', '#.keyIdentifier', _parent); |
228 | 228 |
229 int get _charCode => charCode; | 229 int get _charCode => charCode; |
230 int get _keyCode => keyCode; | 230 int get _keyCode => keyCode; |
231 int get _which => which; | 231 int get _which => which; |
232 | 232 |
233 String get _keyIdentifier { | 233 String get _keyIdentifier { |
234 throw new UnsupportedError("keyIdentifier is unsupported."); | 234 throw new UnsupportedError("keyIdentifier is unsupported."); |
235 } | 235 } |
236 | 236 |
237 void _initKeyboardEvent( | 237 void _initKeyboardEvent( |
238 String type, | 238 String type, |
239 bool canBubble, | 239 bool canBubble, |
240 bool cancelable, | 240 bool cancelable, |
241 Window view, | 241 Window view, |
242 String keyIdentifier, | 242 String keyIdentifier, |
243 int keyLocation, | 243 int location, |
244 bool ctrlKey, | 244 bool ctrlKey, |
245 bool altKey, | 245 bool altKey, |
246 bool shiftKey, | 246 bool shiftKey, |
247 bool metaKey) { | 247 bool metaKey) { |
248 throw new UnsupportedError( | 248 throw new UnsupportedError( |
249 "Cannot initialize a KeyboardEvent from a KeyEvent."); | 249 "Cannot initialize a KeyboardEvent from a KeyEvent."); |
250 } | 250 } |
251 | 251 |
252 @Experimental() // untriaged | 252 @Experimental() // untriaged |
253 bool getModifierState(String keyArgument) => throw new UnimplementedError(); | 253 bool getModifierState(String keyArgument) => throw new UnimplementedError(); |
254 @Experimental() // untriaged | 254 |
255 int get location => throw new UnimplementedError(); | |
256 @Experimental() // untriaged | 255 @Experimental() // untriaged |
257 bool get repeat => throw new UnimplementedError(); | 256 bool get repeat => throw new UnimplementedError(); |
258 dynamic get _get_view => throw new UnimplementedError(); | 257 dynamic get _get_view => throw new UnimplementedError(); |
259 } | 258 } |
OLD | NEW |