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
3 // found in the LICENSE file.
5 #ifndef ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_
6 #define ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_
8 #include "ash/ash_export.h"
9 #include "ash/sticky_keys/sticky_keys_state.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "ui/events/event_constants.h"
12 #include "ui/events/event_handler.h"
26 class StickyKeysOverlay;
27 class StickyKeysHandler;
29 // StickyKeysController is an accessibility feature for users to be able to
30 // compose key and mouse event with modifier keys without simultaneous key
31 // press event. Instead they can compose events separately pressing each of the
32 // modifier keys involved.
33 // e.g. Composing Ctrl + T
34 // User Action : The KeyEvent widget will receives
35 // ----------------------------------------------------------
36 // 1. Press Ctrl key : Ctrl Keydown.
37 // 2. Release Ctrl key : No event
38 // 3. Press T key : T keydown event with ctrl modifier.
40 // 5. Release T key : T keyup without ctrl modifier (Windows behavior)
42 // By typing same modifier keys twice, users can generate bunch of modified key
44 // e.g. To focus tabs consistently by Ctrl + 1, Ctrl + 2 ...
45 // User Action : The KeyEvent widget will receives
46 // ----------------------------------------------------------
47 // 1. Press Ctrl key : Ctrl Keydown
48 // 2. Release Ctrl key : No event
49 // 3. Press Ctrl key : No event
50 // 4. Release Ctrl key : No event
51 // 5. Press 1 key : 1 Keydown event with Ctrl modifier.
52 // 6. Release 1 key : 1 Keyup event with Ctrl modifier.
53 // 7. Press 2 key : 2 Keydown event with Ctrl modifier.
54 // 8. Release 2 key : 2 Keyup event with Ctrl modifier.
55 // 9. Press Ctrl key : No event
56 // 10. Release Ctrl key: Ctrl Keyup
58 // In the case of Chrome OS, StickyKeysController supports Shift,Alt,Ctrl
59 // modifiers. Each handling or state is performed independently.
61 // StickyKeysController is disabled by default.
62 class ASH_EXPORT StickyKeysController : public ui::EventHandler {
64 StickyKeysController();
65 virtual ~StickyKeysController();
67 // Activate sticky keys to intercept and modify incoming events.
68 void Enable(bool enabled);
70 void SetModifiersEnabled(bool mod3_enabled, bool altgr_enabled);
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;
77 // Returns the StickyKeyOverlay used by the controller. Ownership is not
79 StickyKeysOverlay* GetOverlayForTest();
82 // Handles keyboard event. Returns true if Sticky key consumes keyboard event.
83 bool HandleKeyEvent(ui::KeyEvent* event);
85 // Handles mouse event. Returns true if sticky key consumes mouse event.
86 bool HandleMouseEvent(ui::MouseEvent* event);
88 // Handles scroll event. Returns true if sticky key consumes scroll event.
89 bool HandleScrollEvent(ui::ScrollEvent* event);
91 // Updates the overlay UI with the current state of the sticky keys.
94 // Whether sticky keys is activated and modifying events.
97 // Whether the current layout has a mod3 key.
100 // Whether the current layout has an altgr key.
103 // Sticky key handlers.
104 scoped_ptr<StickyKeysHandler> shift_sticky_key_;
105 scoped_ptr<StickyKeysHandler> alt_sticky_key_;
106 scoped_ptr<StickyKeysHandler> altgr_sticky_key_;
107 scoped_ptr<StickyKeysHandler> ctrl_sticky_key_;
108 scoped_ptr<StickyKeysHandler> mod3_sticky_key_;
110 scoped_ptr<StickyKeysOverlay> overlay_;
112 DISALLOW_COPY_AND_ASSIGN(StickyKeysController);
115 // StickyKeysHandler handles key event and controls sticky keysfor specific
116 // modifier keys. If monitored keyboard events are recieved, StickyKeysHandler
117 // changes internal state. If non modifier keyboard events or mouse events are
118 // received, StickyKeysHandler will append modifier based on internal state.
119 // For other events, StickyKeysHandler does nothing.
121 // The DISABLED state is default state and any incoming non modifier keyboard
122 // events will not be modified. The ENABLED state is one shot modification
123 // state. Only next keyboard event will be modified. After that, internal state
124 // will be back to DISABLED state with sending modifier keyup event. In the case
125 // of LOCKED state, all incomming keyboard events will be modified. The LOCKED
126 // state will be back to DISABLED state by next monitoring modifier key.
128 // The detailed state flow as follows:
130 // | DISABLED | ENABLED | LOCKED |
131 // ----------------------------------------------------------------|
132 // Modifier KeyDown | noop | noop(*) | noop(*) |
133 // Modifier KeyUp | To ENABLED(*) | To LOCKED(*) | To DISABLED |
134 // Normal KeyDown | noop | To DISABLED(#) | noop(#) |
135 // Normal KeyUp | noop | noop | noop(#) |
136 // Other KeyUp/Down | noop | noop | noop |
137 // Mouse Press | noop | noop(#) | noop(#) |
138 // Mouse Release | noop | To DISABLED(#) | noop(#) |
139 // Mouse Wheel | noop | To DISABLED(#) | noop(#) |
140 // Other Mouse Event| noop | noop | noop |
142 // Here, (*) means key event will be consumed by StickyKeys, and (#) means event
144 class ASH_EXPORT StickyKeysHandler {
146 class StickyKeysHandlerDelegate {
148 StickyKeysHandlerDelegate();
149 virtual ~StickyKeysHandlerDelegate();
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;
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;
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;
167 // This class takes an ownership of |delegate|.
168 StickyKeysHandler(ui::EventFlags modifier_flag,
169 StickyKeysHandlerDelegate* delegate);
170 ~StickyKeysHandler();
172 // Handles key event. Returns true if key is consumed.
173 bool HandleKeyEvent(ui::KeyEvent* event);
175 // Handles a mouse event. Returns true if mouse event is consumed.
176 bool HandleMouseEvent(ui::MouseEvent* event);
178 // Handles a scroll event. Returns true if scroll event is consumed.
179 bool HandleScrollEvent(ui::ScrollEvent* event);
181 // Returns current internal state.
182 StickyKeyState current_state() const { return current_state_; }
185 // Represents event type in Sticky Key context.
187 TARGET_MODIFIER_DOWN, // The monitoring modifier key is down.
188 TARGET_MODIFIER_UP, // The monitoring modifier key is up.
189 NORMAL_KEY_DOWN, // The non modifier key is down.
190 NORMAL_KEY_UP, // The non modifier key is up.
191 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.
195 // Translates |event| to sticky keys event type.
196 KeyEventType TranslateKeyEvent(ui::KeyEvent* event);
198 // Handles key event in DISABLED state.
199 bool HandleDisabledState(ui::KeyEvent* event);
201 // Handles key event in ENABLED state.
202 bool HandleEnabledState(ui::KeyEvent* event);
204 // Handles key event in LOCKED state.
205 bool HandleLockedState(ui::KeyEvent* event);
207 // Dispatches |event| to its target and then dispatch a key released event
208 // for the modifier key. This function is required to ensure that the events
209 // are sent in the correct order when disabling sticky key after a key/mouse
211 void DispatchEventAndReleaseModifier(ui::Event* event);
213 // Adds |modifier_flags_| to a native X11 event state mask.
214 void AppendNativeEventMask(unsigned int* state);
216 // Adds |modifier_flags_| into |event|.
217 void AppendModifier(ui::KeyEvent* event);
218 void AppendModifier(ui::MouseEvent* event);
219 void AppendModifier(ui::ScrollEvent* event);
221 // The modifier flag to be monitored and appended to events.
222 const ui::EventFlags modifier_flag_;
224 // The current sticky key status.
225 StickyKeyState current_state_;
227 // True if the received key event is sent by StickyKeyHandler.
228 bool event_from_myself_;
230 // 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
232 // shortcuts (eg. ctrl + t) during this time will prevent a transition to
233 // the ENABLED state.
234 bool preparing_to_enable_;
236 // Tracks the scroll direction of the current scroll sequence. Sticky keys
237 // stops modifying the scroll events of the sequence when the direction
238 // changes. If no sequence is tracked, the value is 0.
241 // The modifier up key event to be sent on non modifier key on ENABLED state.
242 scoped_ptr<ui::KeyEvent> modifier_up_event_;
244 scoped_ptr<StickyKeysHandlerDelegate> delegate_;
246 DISALLOW_COPY_AND_ASSIGN(StickyKeysHandler);
251 #endif // ASH_STICKY_KEYS_STICKY_KEYS_CONTROLLER_H_