1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
ash / accelerators / accelerator_capslock_state_machine.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/accelerators/accelerator_capslock_state_machine.h"
#include "base/containers/fixed_flat_set.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/events/types/event_type.h"
#include "ui/ozone/public/input_controller.h"
#include "ui/ozone/public/ozone_platform.h"
namespace ash {
constexpr auto kMetaKeys =
base::MakeFixedFlatSet<ui::KeyboardCode>({ui::VKEY_LWIN, ui::VKEY_RWIN});
constexpr auto kAltKeys =
base::MakeFixedFlatSet<ui::KeyboardCode>({ui::VKEY_MENU});
AcceleratorCapslockStateMachine::AcceleratorCapslockStateMachine(
ui::InputController* input_controller)
: input_controller_(input_controller) {}
AcceleratorCapslockStateMachine::~AcceleratorCapslockStateMachine() = default;
void AcceleratorCapslockStateMachine::OnKeyEvent(ui::KeyEvent* event) {
if (event->type() != ui::EventType::kKeyReleased &&
event->type() != ui::EventType::kKeyPressed) {
return;
}
switch (current_state_) {
// Waiting for either Alt or Search. Anything else we should move to
// kSuppress.
case CapslockState::kStart:
if (event->type() == ui::EventType::kKeyReleased) {
break;
}
if (kMetaKeys.contains(event->key_code())) {
current_state_ = CapslockState::kWaitingAlt;
break;
}
if (kAltKeys.contains(event->key_code())) {
current_state_ = CapslockState::kWaitingSearch;
break;
}
current_state_ = CapslockState::kSuppress;
break;
// Waiting for Alt to be pressed so we can move to kPrimed.
// Anything besides Alt being pressed means we move to kSuppress.
// kTriggerAlt and kWaitingAlt share the same logic except the accelerator
// cannot be triggered in kWaitingAlt.
case CapslockState::kTriggerAlt:
case CapslockState::kWaitingAlt: {
const bool is_meta_key = kMetaKeys.contains(event->key_code());
const bool is_alt_key = kAltKeys.contains(event->key_code());
if (event->type() == ui::EventType::kKeyReleased) {
// If alt key is released, we go back to kStart.
if (is_meta_key) {
current_state_ = CapslockState::kStart;
break;
}
current_state_ = CapslockState::kSuppress;
break;
}
if (is_alt_key) {
current_state_ = CapslockState::kPrimed;
break;
}
if (is_meta_key) {
current_state_ = CapslockState::kWaitingAlt;
break;
}
current_state_ = CapslockState::kSuppress;
break;
}
// Waiting for Search to be pressed so we can move to kPrimed.
// Anything besides Search being pressed means we move to kSuppress.
// kTriggerSearch and kWaitingSearch share the same logic except the
// accelerator cannot be triggered in kWaitingSearch.
case CapslockState::kTriggerSearch:
case CapslockState::kWaitingSearch: {
const bool is_meta_key = kMetaKeys.contains(event->key_code());
const bool is_alt_key = kAltKeys.contains(event->key_code());
if (event->type() == ui::EventType::kKeyReleased) {
// If alt key is released, we go back to kStart.
if (is_alt_key) {
current_state_ = CapslockState::kStart;
break;
}
current_state_ = CapslockState::kSuppress;
break;
}
if (is_meta_key) {
current_state_ = CapslockState::kPrimed;
break;
}
if (is_alt_key) {
current_state_ = CapslockState::kWaitingSearch;
break;
}
current_state_ = CapslockState::kSuppress;
break;
}
// Waiting for all keys to be released to move back to kStart.
case CapslockState::kSuppress:
if (!ui::OzonePlatform::GetInstance()
->GetInputController()
->AreAnyKeysPressed()) {
current_state_ = CapslockState::kStart;
}
break;
// Waiting for either Search or Alt to be released.
// If Search is released, move to kTriggerSearch.
// If Alt is released, move to kTriggerAlt.
// Anything else, move to kSuppress.
case CapslockState::kPrimed:
const bool is_meta = kMetaKeys.contains(event->key_code());
const bool is_alt = kAltKeys.contains(event->key_code());
if (!is_alt && !is_meta) {
current_state_ = CapslockState::kSuppress;
break;
}
if (event->type() == ui::EventType::kKeyPressed) {
break;
}
if (is_meta) {
current_state_ = CapslockState::kTriggerSearch;
break;
}
current_state_ = CapslockState::kTriggerAlt;
break;
}
}
void AcceleratorCapslockStateMachine::OnMouseEvent(ui::MouseEvent* event) {
if (event->type() != ui::EventType::kMousePressed &&
event->type() != ui::EventType::kMouseReleased) {
return;
}
switch (current_state_) {
// On mouse press, move to kSuppress for any state except kSuppress.
case CapslockState::kStart:
case CapslockState::kWaitingAlt:
case CapslockState::kWaitingSearch:
case CapslockState::kTriggerAlt:
case CapslockState::kTriggerSearch:
case CapslockState::kPrimed:
if (event->type() == ui::EventType::kMousePressed) {
current_state_ = CapslockState::kSuppress;
}
break;
// When in kSuppress, move to kStart if the mouse button is released and
// there are no keys currently being pressed.
case CapslockState::kSuppress:
if (event->type() == ui::EventType::kMouseReleased &&
!ui::OzonePlatform::GetInstance()
->GetInputController()
->AreAnyKeysPressed()) {
current_state_ = CapslockState::kStart;
}
break;
}
}
void AcceleratorCapslockStateMachine::SetCanHandleCapsLockForTesting(
bool can_handle) {
current_state_ =
can_handle ? CapslockState::kTriggerAlt : CapslockState::kStart;
}
} // namespace ash