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

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, 3 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
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 15 matching lines...) Expand all
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
47 /** Construct a KeyEvent with [parent] as the event we're emulating. */ 48 /** Construct a KeyEvent with [parent] as the event we're emulating. */
48 KeyEvent(KeyboardEvent parent): super(parent) { 49 KeyEvent.wrap(KeyboardEvent parent): super(parent) {
49 _parent = parent; 50 _parent = parent;
50 _shadowAltKey = _realAltKey; 51 _shadowAltKey = _realAltKey;
51 _shadowCharCode = _realCharCode; 52 _shadowCharCode = _realCharCode;
52 _shadowKeyCode = _realKeyCode; 53 _shadowKeyCode = _realKeyCode;
53 } 54 }
54 55
56 /** Programmatically create a new KeyEvent (and KeyboardEvent). */
57 factory KeyEvent(String type,
58 {Window view, bool canBubble: true, bool cancelable: true, int keyCode: 0,
59 int charCode: 0, int keyLocation: 1, bool ctrlKey: false,
60 bool altKey: false, bool shiftKey: false, bool metaKey: false,
61 bool altGraphKey: false}) {
62 if (view == null) {
blois 2013/09/09 21:13:15 If this event cannot be dispatched, why not have a
63 view = window;
64 }
65
66 var eventObj;
67 if (canUseDispatchEvent) {
68 // Currently works in everything but Internet Explorer.
69 eventObj = new Event.eventType('Event', type,
70 canBubble: canBubble, cancelable: cancelable);
71
72 JS('void', '#.keyCode = #', eventObj, keyCode);
73 JS('void', '#.which = #', eventObj, keyCode);
74 JS('void', '#.charCode = #', eventObj, charCode);
75
76 JS('void', '#.keyLocation = #', eventObj, keyLocation);
77 JS('void', '#.ctrlKey = #', eventObj, ctrlKey);
78 JS('void', '#.altKey = #', eventObj, altKey);
79 JS('void', '#.shiftKey = #', eventObj, shiftKey);
80 JS('void', '#.metaKey = #', eventObj, metaKey);
81 JS('void', '#.altGraphKey = #', eventObj, altGraphKey);
82 } else {
83 // Currently this works on everything but Safari. Safari throws an
84 // "Attempting to change access mechanism for an unconfigurable property"
85 // TypeError when trying to do the Object.defineProperty hack, so we avoid
86 // this branch if possible.
87 // Also, if we want this branch to work in FF, we also need to modify
88 // _initKeyboardEvent to also take charCode and keyCode values to
89 // initialize initKeyEvent.
90
91 eventObj = new Event.eventType('KeyboardEvent', type,
92 canBubble: canBubble, cancelable: cancelable);
93
94 // Chromium Hack
95 JS('void', "Object.defineProperty(#, 'keyCode', {"
96 " get : function() { return this.keyCodeVal; } })", eventObj);
97 JS('void', "Object.defineProperty(#, 'which', {"
98 " get : function() { return this.keyCodeVal; } })", eventObj);
99 JS('void', "Object.defineProperty(#, 'charCode', {"
100 " get : function() { return this.charCodeVal; } })", eventObj);
101
102 var keyIdentifier = _convertToHexString(charCode, keyCode);
103 eventObj._initKeyboardEvent(type, canBubble, cancelable, view,
104 keyIdentifier, keyLocation, ctrlKey, altKey, shiftKey, metaKey,
105 altGraphKey);
106 JS('void', '#.keyCodeVal = #', eventObj, keyCode);
107 JS('void', '#.charCodeVal = #', eventObj, charCode);
108 }
109 // Tell dart2js that it smells like a KeyboardEvent!
110 var interceptor = new KeyboardEvent._private();
blois 2013/09/09 21:13:15 Is there any way to use the JS constructor to get
Emily Fortuna 2013/10/04 23:41:30 No. Stephen and I worked on this, and this was the
sra1 2013/10/07 17:47:11 It would certainly be handy to have a special way
Emily Fortuna 2013/10/07 18:43:04 Haven't run into other instances just yet, but I c
111 var record = makeLeafDispatchRecord(interceptor);
Emily Fortuna 2013/09/04 00:08:08 Stephen and I worked on this wackiness. It was the
112 setDispatchProperty(eventObj, record);
113
114 return new KeyEvent.wrap(eventObj);
115 }
116
117 // Currently known to work on all browsers but IE.
118 static bool get canUseDispatchEvent =>
119 JS('bool', '(typeof document.body.dispatchEvent == "function")'
120 '&& document.body.dispatchEvent.length > 0');
121
122 // This is an experimental method to be sure.
123 static String _convertToHexString(int charCode, int keyCode) {
124 if (charCode != -1) {
125 var hex = charCode.toRadixString(16); // Convert to hexadecimal.
126 StringBuffer sb = new StringBuffer('U+');
127 for (int i = 0; i < 4 - hex.length; i++) sb.write('0');
128 sb.write(hex);
129 return sb.toString();
130 } else {
131 return KeyCode._convertKeyCodeToKeyName(keyCode);
132 }
133 }
134
55 // TODO(efortuna): If KeyEvent is sufficiently successful that we want to make 135 // 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. 136 // it the default keyboard event handling, move these methods over to Element.
57 /** Accessor to provide a stream of KeyEvents on the desired target. */ 137 /** Accessor to provide a stream of KeyEvents on the desired target. */
58 static EventStreamProvider<KeyEvent> keyDownEvent = 138 static EventStreamProvider<KeyEvent> keyDownEvent =
59 new _KeyboardEventHandler('keydown'); 139 new _KeyboardEventHandler('keydown');
60 /** Accessor to provide a stream of KeyEvents on the desired target. */ 140 /** Accessor to provide a stream of KeyEvents on the desired target. */
61 static EventStreamProvider<KeyEvent> keyUpEvent = 141 static EventStreamProvider<KeyEvent> keyUpEvent =
62 new _KeyboardEventHandler('keyup'); 142 new _KeyboardEventHandler('keyup');
63 /** Accessor to provide a stream of KeyEvents on the desired target. */ 143 /** Accessor to provide a stream of KeyEvents on the desired target. */
64 static EventStreamProvider<KeyEvent> keyPressEvent = 144 static EventStreamProvider<KeyEvent> keyPressEvent =
65 new _KeyboardEventHandler('keypress'); 145 new _KeyboardEventHandler('keypress');
66 146
67 /** True if the altGraphKey is pressed during this event. */ 147 /** True if the altGraphKey is pressed during this event. */
68 bool get altGraphKey => _parent.altGraphKey; 148 bool get altGraphKey => _parent.altGraphKey;
69 /** Accessor to the clipboardData available for this event. */ 149 /** Accessor to the clipboardData available for this event. */
70 DataTransfer get clipboardData => _parent.clipboardData; 150 DataTransfer get clipboardData => _parent.clipboardData;
71 /** True if the ctrl key is pressed during this event. */ 151 /** True if the ctrl key is pressed during this event. */
72 bool get ctrlKey => _parent.ctrlKey; 152 bool get ctrlKey => _parent.ctrlKey;
73 int get detail => _parent.detail; 153 int get detail => _parent.detail;
74 /** 154 /**
75 * Accessor to the part of the keyboard that the key was pressed from (one of 155 * 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."); 176 throw new UnsupportedError("keyIdentifier is unsupported.");
97 } 177 }
98 void _initKeyboardEvent(String type, bool canBubble, bool cancelable, 178 void _initKeyboardEvent(String type, bool canBubble, bool cancelable,
99 Window view, String keyIdentifier, int keyLocation, bool ctrlKey, 179 Window view, String keyIdentifier, int keyLocation, bool ctrlKey,
100 bool altKey, bool shiftKey, bool metaKey, 180 bool altKey, bool shiftKey, bool metaKey,
101 bool altGraphKey) { 181 bool altGraphKey) {
102 throw new UnsupportedError( 182 throw new UnsupportedError(
103 "Cannot initialize a KeyboardEvent from a KeyEvent."); 183 "Cannot initialize a KeyboardEvent from a KeyEvent.");
104 } 184 }
105 } 185 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698