Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: tools/dom/src/dart2js_KeyEvent.dart

Issue 23455033: Fully polyfill KeyEvent so that you can programmatically create your own "keyboard" events. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/dom/src/KeyboardEventStream.dart ('k') | tools/dom/src/dartium_KeyEvent.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 * This class is very much a work in progress, and we'd love to get information 9 * This class is very much a work in progress, and we'd love to get information
10 * on how we can make this class work with as many international keyboards as 10 * on how we can make this class work with as many international keyboards as
11 * possible. Bugs welcome! 11 * possible. Bugs welcome!
12 */ 12 */
13 @Experimental()
13 class KeyEvent extends _WrappedEvent implements KeyboardEvent { 14 class KeyEvent extends _WrappedEvent implements KeyboardEvent {
14 /** The parent KeyboardEvent that this KeyEvent is wrapping and "fixing". */ 15 /** The parent KeyboardEvent that this KeyEvent is wrapping and "fixing". */
15 KeyboardEvent _parent; 16 KeyboardEvent _parent;
16 17
17 /** The "fixed" value of whether the alt key is being pressed. */ 18 /** The "fixed" value of whether the alt key is being pressed. */
18 bool _shadowAltKey; 19 bool _shadowAltKey;
19 20
20 /** Caculated value of what the estimated charCode is for this event. */ 21 /** Caculated value of what the estimated charCode is for this event. */
21 int _shadowCharCode; 22 int _shadowCharCode;
22 23
(...skipping 14 matching lines...) Expand all
37 38
38 /** Accessor to the underlying keyCode value is the parent event. */ 39 /** Accessor to the underlying keyCode value is the parent event. */
39 int get _realKeyCode => JS('int', '#.keyCode', _parent); 40 int get _realKeyCode => JS('int', '#.keyCode', _parent);
40 41
41 /** Accessor to the underlying charCode value is the parent event. */ 42 /** Accessor to the underlying charCode value is the parent event. */
42 int get _realCharCode => JS('int', '#.charCode', _parent); 43 int get _realCharCode => JS('int', '#.charCode', _parent);
43 44
44 /** Accessor to the underlying altKey value is the parent event. */ 45 /** Accessor to the underlying altKey value is the parent event. */
45 bool get _realAltKey => JS('bool', '#.altKey', _parent); 46 bool get _realAltKey => JS('bool', '#.altKey', _parent);
46 47
48 /** Shadows on top of the parent's currentTarget. */
49 EventTarget _currentTarget;
50
47 /** Construct a KeyEvent with [parent] as the event we're emulating. */ 51 /** Construct a KeyEvent with [parent] as the event we're emulating. */
48 KeyEvent(KeyboardEvent parent): super(parent) { 52 KeyEvent.wrap(KeyboardEvent parent): super(parent) {
49 _parent = parent; 53 _parent = parent;
50 _shadowAltKey = _realAltKey; 54 _shadowAltKey = _realAltKey;
51 _shadowCharCode = _realCharCode; 55 _shadowCharCode = _realCharCode;
52 _shadowKeyCode = _realKeyCode; 56 _shadowKeyCode = _realKeyCode;
57 _currentTarget = _parent.currentTarget;
58 }
59
60 /** Programmatically create a new KeyEvent (and KeyboardEvent). */
61 factory KeyEvent(String type,
62 {Window view, bool canBubble: true, bool cancelable: true, int keyCode: 0,
63 int charCode: 0, int keyLocation: 1, bool ctrlKey: false,
64 bool altKey: false, bool shiftKey: false, bool metaKey: false,
65 bool altGraphKey: false, EventTarget currentTarget}) {
66 if (view == null) {
67 view = window;
68 }
69
70 var eventObj;
71 // In these two branches we create an underlying native KeyboardEvent, but
72 // we set it with our specified values. Because we are doing custom setting
73 // of certain values (charCode/keyCode, etc) only in this class (as opposed
74 // to KeyboardEvent) and the way we set these custom values depends on the
75 // type of underlying JS object, we do all the contruction for the
76 // underlying KeyboardEvent here.
77 if (canUseDispatchEvent) {
78 // Currently works in everything but Internet Explorer.
79 eventObj = new Event.eventType('Event', type,
80 canBubble: canBubble, cancelable: cancelable);
81
82 JS('void', '#.keyCode = #', eventObj, keyCode);
83 JS('void', '#.which = #', eventObj, keyCode);
84 JS('void', '#.charCode = #', eventObj, charCode);
85
86 JS('void', '#.keyLocation = #', eventObj, keyLocation);
87 JS('void', '#.ctrlKey = #', eventObj, ctrlKey);
88 JS('void', '#.altKey = #', eventObj, altKey);
89 JS('void', '#.shiftKey = #', eventObj, shiftKey);
90 JS('void', '#.metaKey = #', eventObj, metaKey);
91 JS('void', '#.altGraphKey = #', eventObj, altGraphKey);
92 } else {
93 // Currently this works on everything but Safari. Safari throws an
94 // "Attempting to change access mechanism for an unconfigurable property"
95 // TypeError when trying to do the Object.defineProperty hack, so we avoid
96 // this branch if possible.
97 // Also, if we want this branch to work in FF, we also need to modify
98 // _initKeyboardEvent to also take charCode and keyCode values to
99 // initialize initKeyEvent.
100
101 eventObj = new Event.eventType('KeyboardEvent', type,
102 canBubble: canBubble, cancelable: cancelable);
103
104 // Chromium Hack
105 JS('void', "Object.defineProperty(#, 'keyCode', {"
106 " get : function() { return this.keyCodeVal; } })", eventObj);
107 JS('void', "Object.defineProperty(#, 'which', {"
108 " get : function() { return this.keyCodeVal; } })", eventObj);
109 JS('void', "Object.defineProperty(#, 'charCode', {"
110 " get : function() { return this.charCodeVal; } })", eventObj);
111
112 var keyIdentifier = _convertToHexString(charCode, keyCode);
113 eventObj._initKeyboardEvent(type, canBubble, cancelable, view,
114 keyIdentifier, keyLocation, ctrlKey, altKey, shiftKey, metaKey,
115 altGraphKey);
116 JS('void', '#.keyCodeVal = #', eventObj, keyCode);
117 JS('void', '#.charCodeVal = #', eventObj, charCode);
118 }
119 // Tell dart2js that it smells like a KeyboardEvent!
120 var interceptor = new KeyboardEvent._private();
sra1 2013/10/07 17:47:11 Add: TODO(13873): Use better way to get intercepto
Emily Fortuna 2013/10/07 18:43:04 Done.
121 var record = makeLeafDispatchRecord(interceptor);
sra1 2013/10/07 17:47:11 We could make 'record' a static final so we create
Emily Fortuna 2013/10/07 18:43:04 Done.
122 setDispatchProperty(eventObj, record);
123
124 var keyEvent = new KeyEvent.wrap(eventObj);
125 if (keyEvent._currentTarget == null) {
126 keyEvent._currentTarget = currentTarget == null ? window : currentTarget;
127 }
128 return keyEvent;
129 }
130
131 // Currently known to work on all browsers but IE.
132 static bool get canUseDispatchEvent =>
133 JS('bool', '(typeof document.body.dispatchEvent == "function")'
134 '&& document.body.dispatchEvent.length > 0');
sra1 2013/10/07 17:47:11 Better to have an argument that is multiline not s
Emily Fortuna 2013/10/07 18:43:04 Done.
135
136 /** The currently registered target for this event. */
137 EventTarget get currentTarget => _currentTarget;
138
139 // This is an experimental method to be sure.
140 static String _convertToHexString(int charCode, int keyCode) {
141 if (charCode != -1) {
142 var hex = charCode.toRadixString(16); // Convert to hexadecimal.
143 StringBuffer sb = new StringBuffer('U+');
144 for (int i = 0; i < 4 - hex.length; i++) sb.write('0');
145 sb.write(hex);
146 return sb.toString();
147 } else {
148 return KeyCode._convertKeyCodeToKeyName(keyCode);
149 }
53 } 150 }
54 151
55 // TODO(efortuna): If KeyEvent is sufficiently successful that we want to make 152 // TODO(efortuna): If KeyEvent is sufficiently successful that we want to make
56 // it the default keyboard event handling, move these methods over to Element. 153 // it the default keyboard event handling, move these methods over to Element.
57 /** Accessor to provide a stream of KeyEvents on the desired target. */ 154 /** Accessor to provide a stream of KeyEvents on the desired target. */
58 static EventStreamProvider<KeyEvent> keyDownEvent = 155 static EventStreamProvider<KeyEvent> keyDownEvent =
59 new _KeyboardEventHandler('keydown'); 156 new _KeyboardEventHandler('keydown');
60 /** Accessor to provide a stream of KeyEvents on the desired target. */ 157 /** Accessor to provide a stream of KeyEvents on the desired target. */
61 static EventStreamProvider<KeyEvent> keyUpEvent = 158 static EventStreamProvider<KeyEvent> keyUpEvent =
62 new _KeyboardEventHandler('keyup'); 159 new _KeyboardEventHandler('keyup');
63 /** Accessor to provide a stream of KeyEvents on the desired target. */ 160 /** Accessor to provide a stream of KeyEvents on the desired target. */
64 static EventStreamProvider<KeyEvent> keyPressEvent = 161 static EventStreamProvider<KeyEvent> keyPressEvent =
65 new _KeyboardEventHandler('keypress'); 162 new _KeyboardEventHandler('keypress');
66 163
67 /** True if the altGraphKey is pressed during this event. */ 164 /** True if the altGraphKey is pressed during this event. */
68 bool get altGraphKey => _parent.altGraphKey; 165 bool get altGraphKey => _parent.altGraphKey;
69 /** Accessor to the clipboardData available for this event. */ 166 /** Accessor to the clipboardData available for this event. */
70 DataTransfer get clipboardData => _parent.clipboardData; 167 DataTransfer get clipboardData => _parent.clipboardData;
71 /** True if the ctrl key is pressed during this event. */ 168 /** True if the ctrl key is pressed during this event. */
72 bool get ctrlKey => _parent.ctrlKey; 169 bool get ctrlKey => _parent.ctrlKey;
73 int get detail => _parent.detail; 170 int get detail => _parent.detail;
74 /** 171 /**
75 * Accessor to the part of the keyboard that the key was pressed from (one of 172 * Accessor to the part of the keyboard that the key was pressed from (one of
(...skipping 20 matching lines...) Expand all
96 throw new UnsupportedError("keyIdentifier is unsupported."); 193 throw new UnsupportedError("keyIdentifier is unsupported.");
97 } 194 }
98 void _initKeyboardEvent(String type, bool canBubble, bool cancelable, 195 void _initKeyboardEvent(String type, bool canBubble, bool cancelable,
99 Window view, String keyIdentifier, int keyLocation, bool ctrlKey, 196 Window view, String keyIdentifier, int keyLocation, bool ctrlKey,
100 bool altKey, bool shiftKey, bool metaKey, 197 bool altKey, bool shiftKey, bool metaKey,
101 bool altGraphKey) { 198 bool altGraphKey) {
102 throw new UnsupportedError( 199 throw new UnsupportedError(
103 "Cannot initialize a KeyboardEvent from a KeyEvent."); 200 "Cannot initialize a KeyboardEvent from a KeyEvent.");
104 } 201 }
105 } 202 }
OLDNEW
« no previous file with comments | « tools/dom/src/KeyboardEventStream.dart ('k') | tools/dom/src/dartium_KeyEvent.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698