| OLD | NEW |
| (Empty) |
| 1 /** | |
| 2 * A custom KeyboardEvent that attempts to eliminate cross-browser | |
| 3 * inconsistencies, and also provide both keyCode and charCode information | |
| 4 * for all key events (when such information can be determined). | |
| 5 * | |
| 6 * KeyEvent tries to provide a higher level, more polished keyboard event | |
| 7 * information on top of the "raw" [KeyboardEvent]. | |
| 8 * | |
| 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 | |
| 11 * KeyEvents to the stream, rather than using the [EventTarget.dispatchEvent]. | |
| 12 * Here's an example usage: | |
| 13 * | |
| 14 * // Initialize a stream for the KeyEvents: | |
| 15 * var stream = KeyEvent.keyPressEvent.forTarget(document.body); | |
| 16 * // Start listening to the stream of KeyEvents. | |
| 17 * stream.listen((keyEvent) => | |
| 18 * window.console.log('KeyPress event detected ${keyEvent.charCode}')); | |
| 19 * ... | |
| 20 * // Add a new KeyEvent of someone pressing the 'A' key to the stream so | |
| 21 * // listeners can know a KeyEvent happened. | |
| 22 * stream.add(new KeyEvent('keypress', keyCode: 65, charCode: 97)); | |
| 23 * | |
| 24 * This class is very much a work in progress, and we'd love to get information | |
| 25 * on how we can make this class work with as many international keyboards as | |
| 26 * possible. Bugs welcome! | |
| 27 */ | |
| 28 part of html; | |
| 29 | |
| 30 @Experimental() | |
| 31 class KeyEvent extends _WrappedEvent implements KeyboardEvent { | |
| 32 /** Needed because KeyboardEvent is implements. | |
| 33 */ | |
| 34 /** The parent KeyboardEvent that this KeyEvent is wrapping and "fixing". */ | |
| 35 KeyboardEvent _parent; | |
| 36 | |
| 37 /** The "fixed" value of whether the alt key is being pressed. */ | |
| 38 bool _shadowAltKey; | |
| 39 | |
| 40 /** Calculated value of what the estimated charCode is for this event. */ | |
| 41 int _shadowCharCode; | |
| 42 | |
| 43 /** Calculated value of what the estimated keyCode is for this event. */ | |
| 44 int _shadowKeyCode; | |
| 45 | |
| 46 /** Calculated value of what the estimated keyCode is for this event. */ | |
| 47 int get keyCode => _shadowKeyCode; | |
| 48 | |
| 49 /** Calculated value of what the estimated charCode is for this event. */ | |
| 50 int get charCode => this.type == 'keypress' ? _shadowCharCode : 0; | |
| 51 | |
| 52 /** Calculated value of whether the alt key is pressed is for this event. */ | |
| 53 bool get altKey => _shadowAltKey; | |
| 54 | |
| 55 /** Calculated value of what the estimated keyCode is for this event. */ | |
| 56 int get which => keyCode; | |
| 57 | |
| 58 /** Accessor to the underlying keyCode value is the parent event. */ | |
| 59 int get _realKeyCode => _parent.keyCode; | |
| 60 | |
| 61 /** Accessor to the underlying charCode value is the parent event. */ | |
| 62 int get _realCharCode => _parent.charCode; | |
| 63 | |
| 64 /** Accessor to the underlying altKey value is the parent event. */ | |
| 65 bool get _realAltKey => _parent.altKey; | |
| 66 | |
| 67 /** Shadows on top of the parent's currentTarget. */ | |
| 68 EventTarget _currentTarget; | |
| 69 | |
| 70 final InputDeviceCapabilities sourceCapabilities; | |
| 71 | |
| 72 /** Construct a KeyEvent with [parent] as the event we're emulating. */ | |
| 73 KeyEvent.wrap(KeyboardEvent parent) : super(parent) { | |
| 74 _parent = parent; | |
| 75 _shadowAltKey = _realAltKey; | |
| 76 _shadowCharCode = _realCharCode; | |
| 77 _shadowKeyCode = _realKeyCode; | |
| 78 _currentTarget = | |
| 79 _parent.currentTarget == null ? window : _parent.currentTarget; | |
| 80 } | |
| 81 | |
| 82 /** Programmatically create a new KeyEvent (and KeyboardEvent). */ | |
| 83 factory KeyEvent(String type, | |
| 84 {Window view, | |
| 85 bool canBubble: true, | |
| 86 bool cancelable: true, | |
| 87 int keyCode: 0, | |
| 88 int charCode: 0, | |
| 89 int keyLocation: 1, | |
| 90 bool ctrlKey: false, | |
| 91 bool altKey: false, | |
| 92 bool shiftKey: false, | |
| 93 bool metaKey: false, | |
| 94 EventTarget currentTarget}) { | |
| 95 var parent = new KeyboardEvent(type, | |
| 96 view: view, | |
| 97 canBubble: canBubble, | |
| 98 cancelable: cancelable, | |
| 99 keyLocation: keyLocation, | |
| 100 ctrlKey: ctrlKey, | |
| 101 altKey: altKey, | |
| 102 shiftKey: shiftKey, | |
| 103 metaKey: metaKey); | |
| 104 var keyEvent = new KeyEvent.wrap(parent); | |
| 105 keyEvent._shadowAltKey = altKey; | |
| 106 keyEvent._shadowCharCode = charCode; | |
| 107 keyEvent._shadowKeyCode = keyCode; | |
| 108 keyEvent._currentTarget = currentTarget == null ? window : currentTarget; | |
| 109 return keyEvent; | |
| 110 } | |
| 111 | |
| 112 /** Accessor to provide a stream of KeyEvents on the desired target. */ | |
| 113 static EventStreamProvider<KeyEvent> keyDownEvent = | |
| 114 new _KeyboardEventHandler('keydown'); | |
| 115 /** Accessor to provide a stream of KeyEvents on the desired target. */ | |
| 116 static EventStreamProvider<KeyEvent> keyUpEvent = | |
| 117 new _KeyboardEventHandler('keyup'); | |
| 118 /** Accessor to provide a stream of KeyEvents on the desired target. */ | |
| 119 static EventStreamProvider<KeyEvent> keyPressEvent = | |
| 120 new _KeyboardEventHandler('keypress'); | |
| 121 | |
| 122 /** The currently registered target for this event. */ | |
| 123 EventTarget get currentTarget => _currentTarget; | |
| 124 | |
| 125 /** True if the ctrl key is pressed during this event. */ | |
| 126 bool get ctrlKey => _parent.ctrlKey; | |
| 127 int get detail => _parent.detail; | |
| 128 /** | |
| 129 * Accessor to the part of the keyboard that the key was pressed from (one of | |
| 130 * KeyLocation.STANDARD, KeyLocation.RIGHT, KeyLocation.LEFT, | |
| 131 * KeyLocation.NUMPAD, KeyLocation.MOBILE, KeyLocation.JOYSTICK). | |
| 132 */ | |
| 133 int get keyLocation => _parent.keyLocation; | |
| 134 /** True if the Meta (or Mac command) key is pressed during this event. */ | |
| 135 bool get metaKey => _parent.metaKey; | |
| 136 /** True if the shift key was pressed during this event. */ | |
| 137 bool get shiftKey => _parent.shiftKey; | |
| 138 Window get view => _parent.view; | |
| 139 void _initUIEvent( | |
| 140 String type, bool canBubble, bool cancelable, Window view, int detail) { | |
| 141 throw new UnsupportedError("Cannot initialize a UI Event from a KeyEvent."); | |
| 142 } | |
| 143 | |
| 144 String get _shadowKeyIdentifier => _parent._keyIdentifier; | |
| 145 | |
| 146 int get _charCode => charCode; | |
| 147 int get _keyCode => keyCode; | |
| 148 int get _which => which; | |
| 149 String get _keyIdentifier { | |
| 150 throw new UnsupportedError("keyIdentifier is unsupported."); | |
| 151 } | |
| 152 | |
| 153 void _initKeyboardEvent( | |
| 154 String type, | |
| 155 bool canBubble, | |
| 156 bool cancelable, | |
| 157 Window view, | |
| 158 String keyIdentifier, | |
| 159 int keyLocation, | |
| 160 bool ctrlKey, | |
| 161 bool altKey, | |
| 162 bool shiftKey, | |
| 163 bool metaKey) { | |
| 164 throw new UnsupportedError( | |
| 165 "Cannot initialize a KeyboardEvent from a KeyEvent."); | |
| 166 } | |
| 167 | |
| 168 @Experimental() // untriaged | |
| 169 bool getModifierState(String keyArgument) => throw new UnimplementedError(); | |
| 170 @Experimental() // untriaged | |
| 171 int get location => throw new UnimplementedError(); | |
| 172 @Experimental() // untriaged | |
| 173 bool get repeat => throw new UnimplementedError(); | |
| 174 dynamic get _get_view => throw new UnimplementedError(); | |
| 175 } | |
| OLD | NEW |