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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 _shadowCharCode = _realCharCode; | 82 _shadowCharCode = _realCharCode; |
83 _shadowKeyCode = _realKeyCode; | 83 _shadowKeyCode = _realKeyCode; |
84 _currentTarget = _parent.currentTarget; | 84 _currentTarget = _parent.currentTarget; |
85 } | 85 } |
86 | 86 |
87 /** Programmatically create a new KeyEvent (and KeyboardEvent). */ | 87 /** Programmatically create a new KeyEvent (and KeyboardEvent). */ |
88 factory KeyEvent(String type, | 88 factory KeyEvent(String type, |
89 {Window view, bool canBubble: true, bool cancelable: true, int keyCode: 0, | 89 {Window view, bool canBubble: true, bool cancelable: true, int keyCode: 0, |
90 int charCode: 0, int keyLocation: 1, bool ctrlKey: false, | 90 int charCode: 0, int keyLocation: 1, bool ctrlKey: false, |
91 bool altKey: false, bool shiftKey: false, bool metaKey: false, | 91 bool altKey: false, bool shiftKey: false, bool metaKey: false, |
92 bool altGraphKey: false, EventTarget currentTarget}) { | 92 EventTarget currentTarget}) { |
93 if (view == null) { | 93 if (view == null) { |
94 view = window; | 94 view = window; |
95 } | 95 } |
96 | 96 |
97 var eventObj; | 97 var eventObj; |
98 // In these two branches we create an underlying native KeyboardEvent, but | 98 // In these two branches we create an underlying native KeyboardEvent, but |
99 // we set it with our specified values. Because we are doing custom setting | 99 // we set it with our specified values. Because we are doing custom setting |
100 // of certain values (charCode/keyCode, etc) only in this class (as opposed | 100 // of certain values (charCode/keyCode, etc) only in this class (as opposed |
101 // to KeyboardEvent) and the way we set these custom values depends on the | 101 // to KeyboardEvent) and the way we set these custom values depends on the |
102 // type of underlying JS object, we do all the contruction for the | 102 // type of underlying JS object, we do all the contruction for the |
103 // underlying KeyboardEvent here. | 103 // underlying KeyboardEvent here. |
104 if (canUseDispatchEvent) { | 104 if (canUseDispatchEvent) { |
105 // Currently works in everything but Internet Explorer. | 105 // Currently works in everything but Internet Explorer. |
106 eventObj = new Event.eventType('Event', type, | 106 eventObj = new Event.eventType('Event', type, |
107 canBubble: canBubble, cancelable: cancelable); | 107 canBubble: canBubble, cancelable: cancelable); |
108 | 108 |
109 JS('void', '#.keyCode = #', eventObj, keyCode); | 109 JS('void', '#.keyCode = #', eventObj, keyCode); |
110 JS('void', '#.which = #', eventObj, keyCode); | 110 JS('void', '#.which = #', eventObj, keyCode); |
111 JS('void', '#.charCode = #', eventObj, charCode); | 111 JS('void', '#.charCode = #', eventObj, charCode); |
112 | 112 |
113 JS('void', '#.keyLocation = #', eventObj, keyLocation); | 113 JS('void', '#.keyLocation = #', eventObj, keyLocation); |
114 JS('void', '#.ctrlKey = #', eventObj, ctrlKey); | 114 JS('void', '#.ctrlKey = #', eventObj, ctrlKey); |
115 JS('void', '#.altKey = #', eventObj, altKey); | 115 JS('void', '#.altKey = #', eventObj, altKey); |
116 JS('void', '#.shiftKey = #', eventObj, shiftKey); | 116 JS('void', '#.shiftKey = #', eventObj, shiftKey); |
117 JS('void', '#.metaKey = #', eventObj, metaKey); | 117 JS('void', '#.metaKey = #', eventObj, metaKey); |
118 JS('void', '#.altGraphKey = #', eventObj, altGraphKey); | |
119 } else { | 118 } else { |
120 // Currently this works on everything but Safari. Safari throws an | 119 // Currently this works on everything but Safari. Safari throws an |
121 // "Attempting to change access mechanism for an unconfigurable property" | 120 // "Attempting to change access mechanism for an unconfigurable property" |
122 // TypeError when trying to do the Object.defineProperty hack, so we avoid | 121 // TypeError when trying to do the Object.defineProperty hack, so we avoid |
123 // this branch if possible. | 122 // this branch if possible. |
124 // Also, if we want this branch to work in FF, we also need to modify | 123 // Also, if we want this branch to work in FF, we also need to modify |
125 // _initKeyboardEvent to also take charCode and keyCode values to | 124 // _initKeyboardEvent to also take charCode and keyCode values to |
126 // initialize initKeyEvent. | 125 // initialize initKeyEvent. |
127 | 126 |
128 eventObj = new Event.eventType('KeyboardEvent', type, | 127 eventObj = new Event.eventType('KeyboardEvent', type, |
129 canBubble: canBubble, cancelable: cancelable); | 128 canBubble: canBubble, cancelable: cancelable); |
130 | 129 |
131 // Chromium Hack | 130 // Chromium Hack |
132 JS('void', "Object.defineProperty(#, 'keyCode', {" | 131 JS('void', "Object.defineProperty(#, 'keyCode', {" |
133 " get : function() { return this.keyCodeVal; } })", eventObj); | 132 " get : function() { return this.keyCodeVal; } })", eventObj); |
134 JS('void', "Object.defineProperty(#, 'which', {" | 133 JS('void', "Object.defineProperty(#, 'which', {" |
135 " get : function() { return this.keyCodeVal; } })", eventObj); | 134 " get : function() { return this.keyCodeVal; } })", eventObj); |
136 JS('void', "Object.defineProperty(#, 'charCode', {" | 135 JS('void', "Object.defineProperty(#, 'charCode', {" |
137 " get : function() { return this.charCodeVal; } })", eventObj); | 136 " get : function() { return this.charCodeVal; } })", eventObj); |
138 | 137 |
139 var keyIdentifier = _convertToHexString(charCode, keyCode); | 138 var keyIdentifier = _convertToHexString(charCode, keyCode); |
140 eventObj._initKeyboardEvent(type, canBubble, cancelable, view, | 139 eventObj._initKeyboardEvent(type, canBubble, cancelable, view, |
141 keyIdentifier, keyLocation, ctrlKey, altKey, shiftKey, metaKey, | 140 keyIdentifier, keyLocation, ctrlKey, altKey, shiftKey, metaKey); |
142 altGraphKey); | |
143 JS('void', '#.keyCodeVal = #', eventObj, keyCode); | 141 JS('void', '#.keyCodeVal = #', eventObj, keyCode); |
144 JS('void', '#.charCodeVal = #', eventObj, charCode); | 142 JS('void', '#.charCodeVal = #', eventObj, charCode); |
145 } | 143 } |
146 // Tell dart2js that it smells like a KeyboardEvent! | 144 // Tell dart2js that it smells like a KeyboardEvent! |
147 setDispatchProperty(eventObj, _keyboardEventDispatchRecord); | 145 setDispatchProperty(eventObj, _keyboardEventDispatchRecord); |
148 | 146 |
149 var keyEvent = new KeyEvent.wrap(eventObj); | 147 var keyEvent = new KeyEvent.wrap(eventObj); |
150 if (keyEvent._currentTarget == null) { | 148 if (keyEvent._currentTarget == null) { |
151 keyEvent._currentTarget = currentTarget == null ? window : currentTarget; | 149 keyEvent._currentTarget = currentTarget == null ? window : currentTarget; |
152 } | 150 } |
(...skipping 27 matching lines...) Expand all Loading... |
180 /** Accessor to provide a stream of KeyEvents on the desired target. */ | 178 /** Accessor to provide a stream of KeyEvents on the desired target. */ |
181 static EventStreamProvider<KeyEvent> keyDownEvent = | 179 static EventStreamProvider<KeyEvent> keyDownEvent = |
182 new _KeyboardEventHandler('keydown'); | 180 new _KeyboardEventHandler('keydown'); |
183 /** Accessor to provide a stream of KeyEvents on the desired target. */ | 181 /** Accessor to provide a stream of KeyEvents on the desired target. */ |
184 static EventStreamProvider<KeyEvent> keyUpEvent = | 182 static EventStreamProvider<KeyEvent> keyUpEvent = |
185 new _KeyboardEventHandler('keyup'); | 183 new _KeyboardEventHandler('keyup'); |
186 /** Accessor to provide a stream of KeyEvents on the desired target. */ | 184 /** Accessor to provide a stream of KeyEvents on the desired target. */ |
187 static EventStreamProvider<KeyEvent> keyPressEvent = | 185 static EventStreamProvider<KeyEvent> keyPressEvent = |
188 new _KeyboardEventHandler('keypress'); | 186 new _KeyboardEventHandler('keypress'); |
189 | 187 |
190 /** True if the altGraphKey is pressed during this event. */ | |
191 bool get altGraphKey => _parent.altGraphKey; | |
192 /** Accessor to the clipboardData available for this event. */ | 188 /** Accessor to the clipboardData available for this event. */ |
193 DataTransfer get clipboardData => _parent.clipboardData; | 189 DataTransfer get clipboardData => _parent.clipboardData; |
194 /** True if the ctrl key is pressed during this event. */ | 190 /** True if the ctrl key is pressed during this event. */ |
195 bool get ctrlKey => _parent.ctrlKey; | 191 bool get ctrlKey => _parent.ctrlKey; |
196 int get detail => _parent.detail; | 192 int get detail => _parent.detail; |
197 /** | 193 /** |
198 * Accessor to the part of the keyboard that the key was pressed from (one of | 194 * Accessor to the part of the keyboard that the key was pressed from (one of |
199 * KeyLocation.STANDARD, KeyLocation.RIGHT, KeyLocation.LEFT, | 195 * KeyLocation.STANDARD, KeyLocation.RIGHT, KeyLocation.LEFT, |
200 * KeyLocation.NUMPAD, KeyLocation.MOBILE, KeyLocation.JOYSTICK). | 196 * KeyLocation.NUMPAD, KeyLocation.MOBILE, KeyLocation.JOYSTICK). |
201 */ | 197 */ |
(...skipping 11 matching lines...) Expand all Loading... |
213 } | 209 } |
214 String get _shadowKeyIdentifier => JS('String', '#.keyIdentifier', _parent); | 210 String get _shadowKeyIdentifier => JS('String', '#.keyIdentifier', _parent); |
215 | 211 |
216 int get _charCode => charCode; | 212 int get _charCode => charCode; |
217 int get _keyCode => keyCode; | 213 int get _keyCode => keyCode; |
218 String get _keyIdentifier { | 214 String get _keyIdentifier { |
219 throw new UnsupportedError("keyIdentifier is unsupported."); | 215 throw new UnsupportedError("keyIdentifier is unsupported."); |
220 } | 216 } |
221 void _initKeyboardEvent(String type, bool canBubble, bool cancelable, | 217 void _initKeyboardEvent(String type, bool canBubble, bool cancelable, |
222 Window view, String keyIdentifier, int keyLocation, bool ctrlKey, | 218 Window view, String keyIdentifier, int keyLocation, bool ctrlKey, |
223 bool altKey, bool shiftKey, bool metaKey, | 219 bool altKey, bool shiftKey, bool metaKey) { |
224 bool altGraphKey) { | |
225 throw new UnsupportedError( | 220 throw new UnsupportedError( |
226 "Cannot initialize a KeyboardEvent from a KeyEvent."); | 221 "Cannot initialize a KeyboardEvent from a KeyEvent."); |
227 } | 222 } |
228 int get _layerX => throw new UnsupportedError('Not applicable to KeyEvent'); | 223 int get _layerX => throw new UnsupportedError('Not applicable to KeyEvent'); |
229 int get _layerY => throw new UnsupportedError('Not applicable to KeyEvent'); | 224 int get _layerY => throw new UnsupportedError('Not applicable to KeyEvent'); |
230 int get _pageX => throw new UnsupportedError('Not applicable to KeyEvent'); | 225 int get _pageX => throw new UnsupportedError('Not applicable to KeyEvent'); |
231 int get _pageY => throw new UnsupportedError('Not applicable to KeyEvent'); | 226 int get _pageY => throw new UnsupportedError('Not applicable to KeyEvent'); |
232 @Experimental() // untriaged | 227 @Experimental() // untriaged |
233 bool getModifierState(String keyArgument) => throw new UnimplementedError(); | 228 bool getModifierState(String keyArgument) => throw new UnimplementedError(); |
234 @Experimental() // untriaged | 229 @Experimental() // untriaged |
235 int get location => throw new UnimplementedError(); | 230 int get location => throw new UnimplementedError(); |
236 @Experimental() // untriaged | 231 @Experimental() // untriaged |
237 bool get repeat => throw new UnimplementedError(); | 232 bool get repeat => throw new UnimplementedError(); |
238 dynamic get _get_view => throw new UnimplementedError(); | 233 dynamic get _get_view => throw new UnimplementedError(); |
239 } | 234 } |
OLD | NEW |