OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_ | 5 #ifndef ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_ |
6 #define ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_ | 6 #define ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_ |
7 | 7 |
8 #include "ash/ash_export.h" | 8 #include "ash/ash_export.h" |
9 #include "ash/sticky_keys/sticky_keys_state.h" | 9 #include "ash/sticky_keys/sticky_keys_state.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "ui/events/event_constants.h" | 11 #include "ui/events/event_constants.h" |
12 #include "ui/events/event_handler.h" | 12 #include "ui/events/event_handler.h" |
| 13 #include "ui/events/event_rewriter.h" |
| 14 #include "ui/events/keycodes/keyboard_codes.h" |
13 | 15 |
14 namespace ui { | 16 namespace ui { |
15 class Event; | 17 class Event; |
16 class KeyEvent; | 18 class KeyEvent; |
17 class MouseEvent; | 19 class MouseEvent; |
18 } // namespace ui | 20 } // namespace ui |
19 | 21 |
20 namespace aura { | 22 namespace aura { |
21 class Window; | 23 class Window; |
22 } // namespace aura | 24 } // namespace aura |
(...skipping 29 matching lines...) Expand all Loading... |
52 // 6. Release 1 key : 1 Keyup event with Ctrl modifier. | 54 // 6. Release 1 key : 1 Keyup event with Ctrl modifier. |
53 // 7. Press 2 key : 2 Keydown event with Ctrl modifier. | 55 // 7. Press 2 key : 2 Keydown event with Ctrl modifier. |
54 // 8. Release 2 key : 2 Keyup event with Ctrl modifier. | 56 // 8. Release 2 key : 2 Keyup event with Ctrl modifier. |
55 // 9. Press Ctrl key : No event | 57 // 9. Press Ctrl key : No event |
56 // 10. Release Ctrl key: Ctrl Keyup | 58 // 10. Release Ctrl key: Ctrl Keyup |
57 // | 59 // |
58 // In the case of Chrome OS, StickyKeysController supports Shift,Alt,Ctrl | 60 // In the case of Chrome OS, StickyKeysController supports Shift,Alt,Ctrl |
59 // modifiers. Each handling or state is performed independently. | 61 // modifiers. Each handling or state is performed independently. |
60 // | 62 // |
61 // StickyKeysController is disabled by default. | 63 // StickyKeysController is disabled by default. |
62 class ASH_EXPORT StickyKeysController : public ui::EventHandler { | 64 class ASH_EXPORT StickyKeysController { |
63 public: | 65 public: |
64 StickyKeysController(); | 66 StickyKeysController(); |
65 virtual ~StickyKeysController(); | 67 virtual ~StickyKeysController(); |
66 | 68 |
67 // Activate sticky keys to intercept and modify incoming events. | 69 // Activate sticky keys to intercept and modify incoming events. |
68 void Enable(bool enabled); | 70 void Enable(bool enabled); |
69 | 71 |
70 void SetModifiersEnabled(bool mod3_enabled, bool altgr_enabled); | 72 void SetModifiersEnabled(bool mod3_enabled, bool altgr_enabled); |
71 | 73 |
72 // Overridden from ui::EventHandler: | |
73 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE; | |
74 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE; | |
75 virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE; | |
76 | |
77 // Returns the StickyKeyOverlay used by the controller. Ownership is not | 74 // Returns the StickyKeyOverlay used by the controller. Ownership is not |
78 // passed. | 75 // passed. |
79 StickyKeysOverlay* GetOverlayForTest(); | 76 StickyKeysOverlay* GetOverlayForTest(); |
80 | 77 |
| 78 // Handles keyboard event. Returns an |EventRewriteStatus|, and may |
| 79 // modify |flags|: |
| 80 // - Returns ui::EVENT_REWRITE_DISCARD, and leaves |flags| untouched, |
| 81 // if the event is consumed (i.e. a sticky modifier press or release); |
| 82 // - Returns ui::EVENT_REWRITE_REWRITTEN if the event needs to be modified |
| 83 // according to the returned |flags| (i.e. a sticky-modified key); |
| 84 // - Returns ui::EVENT_REWRITE_DISPATCH_ANOTHER if the event needs to be |
| 85 // modified according to the returned |flags|, and there are delayed |
| 86 // modifier-up events now to be retrieved using |NextDispatchEvent()| |
| 87 // (i.e. a sticky-modified key that ends a sticky state); |
| 88 // - Otherwise returns ui::EVENT_REWRITE_CONTINUE and leaves |flags| |
| 89 // unchanged. |
| 90 ui::EventRewriteStatus RewriteKeyEvent(const ui::KeyEvent& event, |
| 91 ui::KeyboardCode key_code, |
| 92 int* flags); |
| 93 |
| 94 // Handles mouse event. |
| 95 ui::EventRewriteStatus RewriteMouseEvent(const ui::MouseEvent& event, |
| 96 int* flags); |
| 97 |
| 98 // Handles scroll event. |
| 99 ui::EventRewriteStatus RewriteScrollEvent(const ui::ScrollEvent& event, |
| 100 int* flags); |
| 101 |
| 102 // Obtains a pending modifier-up event. If the immediately previous |
| 103 // call to |Rewrite...Event()| or |NextDispatchEvent()| returned |
| 104 // ui::EVENT_REWRITE_DISPATCH_ANOTHER, this sets |new_event| and returns: |
| 105 // - ui::EVENT_REWRITE_DISPATCH_ANOTHER if there is at least one more |
| 106 // pending modifier-up event; |
| 107 // - ui::EVENT_REWRITE_REWRITE if this is the last or only modifier-up event; |
| 108 // Otherwise, there is no pending modifier-up event, and this function |
| 109 // returns ui::EVENT_REWRITE_CONTINUE and sets |new_event| to NULL. |
| 110 ui::EventRewriteStatus NextDispatchEvent(scoped_ptr<ui::Event>* new_event); |
| 111 |
81 private: | 112 private: |
82 // Handles keyboard event. Returns true if Sticky key consumes keyboard event. | 113 // Handles keyboard event. Returns true if Sticky key consumes keyboard event. |
83 bool HandleKeyEvent(ui::KeyEvent* event); | 114 // Adds to |mod_down_flags| any flag to be added to the key event. |
| 115 // Sets |released| if any modifier is to be released after the key event. |
| 116 bool HandleKeyEvent(const ui::KeyEvent& event, |
| 117 ui::KeyboardCode key_code, |
| 118 int* mod_down_flags, |
| 119 bool* released); |
84 | 120 |
85 // Handles mouse event. Returns true if sticky key consumes mouse event. | 121 // Handles mouse event. Returns true if Sticky key consumes keyboard event. |
86 bool HandleMouseEvent(ui::MouseEvent* event); | 122 // Sets |released| if any modifier is to be released after the key event. |
| 123 bool HandleMouseEvent(const ui::MouseEvent& event, |
| 124 int* mod_down_flags, |
| 125 bool* released); |
87 | 126 |
88 // Handles scroll event. Returns true if sticky key consumes scroll event. | 127 // Handles scroll event. Returns true if Sticky key consumes keyboard event. |
89 bool HandleScrollEvent(ui::ScrollEvent* event); | 128 // Sets |released| if any modifier is to be released after the key event. |
| 129 bool HandleScrollEvent(const ui::ScrollEvent& event, |
| 130 int* mod_down_flags, |
| 131 bool* released); |
90 | 132 |
91 // Updates the overlay UI with the current state of the sticky keys. | 133 // Updates the overlay UI with the current state of the sticky keys. |
92 void UpdateOverlay(); | 134 void UpdateOverlay(); |
93 | 135 |
94 // Whether sticky keys is activated and modifying events. | 136 // Whether sticky keys is activated and modifying events. |
95 bool enabled_; | 137 bool enabled_; |
96 | 138 |
97 // Whether the current layout has a mod3 key. | 139 // Whether the current layout has a mod3 key. |
98 bool mod3_enabled_; | 140 bool mod3_enabled_; |
99 | 141 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 // Other KeyUp/Down | noop | noop | noop | | 178 // Other KeyUp/Down | noop | noop | noop | |
137 // Mouse Press | noop | noop(#) | noop(#) | | 179 // Mouse Press | noop | noop(#) | noop(#) | |
138 // Mouse Release | noop | To DISABLED(#) | noop(#) | | 180 // Mouse Release | noop | To DISABLED(#) | noop(#) | |
139 // Mouse Wheel | noop | To DISABLED(#) | noop(#) | | 181 // Mouse Wheel | noop | To DISABLED(#) | noop(#) | |
140 // Other Mouse Event| noop | noop | noop | | 182 // Other Mouse Event| noop | noop | noop | |
141 // | 183 // |
142 // Here, (*) means key event will be consumed by StickyKeys, and (#) means event | 184 // Here, (*) means key event will be consumed by StickyKeys, and (#) means event |
143 // is modified. | 185 // is modified. |
144 class ASH_EXPORT StickyKeysHandler { | 186 class ASH_EXPORT StickyKeysHandler { |
145 public: | 187 public: |
146 class StickyKeysHandlerDelegate { | 188 explicit StickyKeysHandler(ui::EventFlags modifier_flag); |
147 public: | |
148 StickyKeysHandlerDelegate(); | |
149 virtual ~StickyKeysHandlerDelegate(); | |
150 | |
151 // Dispatches keyboard event synchronously. |event| is an event that has | |
152 // been previously dispatched. | |
153 virtual void DispatchKeyEvent(ui::KeyEvent* event, | |
154 aura::Window* target) = 0; | |
155 | |
156 // Dispatches mouse event synchronously. |event| is an event that has | |
157 // been previously dispatched. | |
158 virtual void DispatchMouseEvent(ui::MouseEvent* event, | |
159 aura::Window* target) = 0; | |
160 | |
161 // Dispatches scroll event synchronously. |event| is an event that has | |
162 // been previously dispatched. | |
163 virtual void DispatchScrollEvent(ui::ScrollEvent* event, | |
164 aura::Window* target) = 0; | |
165 }; | |
166 | |
167 // This class takes an ownership of |delegate|. | |
168 StickyKeysHandler(ui::EventFlags modifier_flag, | |
169 StickyKeysHandlerDelegate* delegate); | |
170 ~StickyKeysHandler(); | 189 ~StickyKeysHandler(); |
171 | 190 |
172 // Handles key event. Returns true if key is consumed. | 191 // Handles keyboard event. Returns true if Sticky key consumes keyboard event. |
173 bool HandleKeyEvent(ui::KeyEvent* event); | 192 // Sets its own modifier flag in |mod_down_flags| if it is active and needs |
| 193 // to be added to the event, and sets |released| if releasing it. |
| 194 bool HandleKeyEvent(const ui::KeyEvent& event, |
| 195 ui::KeyboardCode key_code, |
| 196 int* mod_down_flags, |
| 197 bool* released); |
174 | 198 |
175 // Handles a mouse event. Returns true if mouse event is consumed. | 199 // Handles mouse event. Returns true if sticky key consumes mouse event. |
176 bool HandleMouseEvent(ui::MouseEvent* event); | 200 // Sets its own modifier flag in |mod_down_flags| if it is active and needs |
| 201 // to be added to the event, and sets |released| if releasing it. |
| 202 bool HandleMouseEvent(const ui::MouseEvent& event, |
| 203 int* mod_down_flags, |
| 204 bool* released); |
177 | 205 |
178 // Handles a scroll event. Returns true if scroll event is consumed. | 206 // Handles scroll event. Returns true if sticky key consumes scroll event. |
179 bool HandleScrollEvent(ui::ScrollEvent* event); | 207 // Sets its own modifier flag in |mod_down_flags| if it is active and needs |
| 208 // to be added to the event, and sets |released| if releasing it. |
| 209 bool HandleScrollEvent(const ui::ScrollEvent& event, |
| 210 int* mod_down_flags, |
| 211 bool* released); |
| 212 |
| 213 // Fetches a pending modifier-up event if one exists and the return |
| 214 // parameter |new_event| is available (i.e. not set). Returns the number |
| 215 // of pending events still remaining to be returned. |
| 216 int GetModifierUpEvent(scoped_ptr<ui::Event>* new_event); |
180 | 217 |
181 // Returns current internal state. | 218 // Returns current internal state. |
182 StickyKeyState current_state() const { return current_state_; } | 219 StickyKeyState current_state() const { return current_state_; } |
183 | 220 |
184 private: | 221 private: |
185 // Represents event type in Sticky Key context. | 222 // Represents event type in Sticky Key context. |
186 enum KeyEventType { | 223 enum KeyEventType { |
187 TARGET_MODIFIER_DOWN, // The monitoring modifier key is down. | 224 TARGET_MODIFIER_DOWN, // The monitoring modifier key is down. |
188 TARGET_MODIFIER_UP, // The monitoring modifier key is up. | 225 TARGET_MODIFIER_UP, // The monitoring modifier key is up. |
189 NORMAL_KEY_DOWN, // The non modifier key is down. | 226 NORMAL_KEY_DOWN, // The non modifier key is down. |
190 NORMAL_KEY_UP, // The non modifier key is up. | 227 NORMAL_KEY_UP, // The non modifier key is up. |
191 OTHER_MODIFIER_DOWN, // The modifier key but not monitored key is down. | 228 OTHER_MODIFIER_DOWN, // The modifier key but not monitored key is down. |
192 OTHER_MODIFIER_UP, // The modifier key but not monitored key is up. | 229 OTHER_MODIFIER_UP, // The modifier key but not monitored key is up. |
193 }; | 230 }; |
194 | 231 |
195 // Translates |event| to sticky keys event type. | 232 // Translates event type and key code to sticky keys event type. |
196 KeyEventType TranslateKeyEvent(ui::KeyEvent* event); | 233 KeyEventType TranslateKeyEvent(ui::EventType type, ui::KeyboardCode key_code); |
197 | 234 |
198 // Handles key event in DISABLED state. | 235 // Handles key event in DISABLED state. Returns true if sticky keys |
199 bool HandleDisabledState(ui::KeyEvent* event); | 236 // consumes the keyboard event. |
| 237 bool HandleDisabledState(const ui::KeyEvent& event, |
| 238 ui::KeyboardCode key_code); |
200 | 239 |
201 // Handles key event in ENABLED state. | 240 // Handles key event in ENABLED state. Returns true if sticky keys |
202 bool HandleEnabledState(ui::KeyEvent* event); | 241 // consumes the keyboard event. |
| 242 bool HandleEnabledState(const ui::KeyEvent& event, |
| 243 ui::KeyboardCode key_code, |
| 244 int* mod_down_flags, |
| 245 bool* released); |
203 | 246 |
204 // Handles key event in LOCKED state. | 247 // Handles key event in LOCKED state. Returns true if sticky keys |
205 bool HandleLockedState(ui::KeyEvent* event); | 248 // consumes the keyboard event. |
206 | 249 bool HandleLockedState(const ui::KeyEvent& event, |
207 // Dispatches |event| to its target and then dispatch a key released event | 250 ui::KeyboardCode key_code, |
208 // for the modifier key. This function is required to ensure that the events | 251 int* mod_down_flags, |
209 // are sent in the correct order when disabling sticky key after a key/mouse | 252 bool* released); |
210 // button press. | |
211 void DispatchEventAndReleaseModifier(ui::Event* event); | |
212 | |
213 // Adds |modifier_flags_| to a native X11 event state mask. | |
214 void AppendNativeEventMask(unsigned int* state); | |
215 | |
216 // Adds |modifier_flags_| into |event|. | |
217 void AppendModifier(ui::KeyEvent* event); | |
218 void AppendModifier(ui::MouseEvent* event); | |
219 void AppendModifier(ui::ScrollEvent* event); | |
220 | 253 |
221 // The modifier flag to be monitored and appended to events. | 254 // The modifier flag to be monitored and appended to events. |
222 const ui::EventFlags modifier_flag_; | 255 const ui::EventFlags modifier_flag_; |
223 | 256 |
224 // The current sticky key status. | 257 // The current sticky key status. |
225 StickyKeyState current_state_; | 258 StickyKeyState current_state_; |
226 | 259 |
227 // True if the received key event is sent by StickyKeyHandler. | |
228 bool event_from_myself_; | |
229 | |
230 // True if we received the TARGET_MODIFIER_DOWN event while in the DISABLED | 260 // True if we received the TARGET_MODIFIER_DOWN event while in the DISABLED |
231 // state but before we receive the TARGET_MODIFIER_UP event. Normal | 261 // state but before we receive the TARGET_MODIFIER_UP event. Normal |
232 // shortcuts (eg. ctrl + t) during this time will prevent a transition to | 262 // shortcuts (eg. ctrl + t) during this time will prevent a transition to |
233 // the ENABLED state. | 263 // the ENABLED state. |
234 bool preparing_to_enable_; | 264 bool preparing_to_enable_; |
235 | 265 |
236 // Tracks the scroll direction of the current scroll sequence. Sticky keys | 266 // Tracks the scroll direction of the current scroll sequence. Sticky keys |
237 // stops modifying the scroll events of the sequence when the direction | 267 // stops modifying the scroll events of the sequence when the direction |
238 // changes. If no sequence is tracked, the value is 0. | 268 // changes. If no sequence is tracked, the value is 0. |
239 int scroll_delta_; | 269 int scroll_delta_; |
240 | 270 |
241 // The modifier up key event to be sent on non modifier key on ENABLED state. | 271 // The modifier up key event to be sent on non modifier key on ENABLED state. |
242 scoped_ptr<ui::KeyEvent> modifier_up_event_; | 272 scoped_ptr<ui::KeyEvent> modifier_up_event_; |
243 | 273 |
244 scoped_ptr<StickyKeysHandlerDelegate> delegate_; | |
245 | |
246 DISALLOW_COPY_AND_ASSIGN(StickyKeysHandler); | 274 DISALLOW_COPY_AND_ASSIGN(StickyKeysHandler); |
247 }; | 275 }; |
248 | 276 |
249 } // namespace ash | 277 } // namespace ash |
250 | 278 |
251 #endif // ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_ | 279 #endif // ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_ |
OLD | NEW |