forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch_access_event_handler.cc
66 lines (50 loc) · 1.71 KB
/
switch_access_event_handler.cc
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/events/switch_access_event_handler.h"
#include "ash/accessibility/accessibility_controller.h"
#include "ash/shell.h"
namespace ash {
namespace {
bool IsSwitchAccessEnabled() {
return Shell::Get()->accessibility_controller()->switch_access_enabled();
}
void CancelEvent(ui::Event* event) {
DCHECK(event);
if (event->cancelable()) {
event->SetHandled();
event->StopPropagation();
}
}
} // namespace
SwitchAccessEventHandler::SwitchAccessEventHandler(
mojom::SwitchAccessEventHandlerDelegatePtr delegate_ptr)
: delegate_ptr_(std::move(delegate_ptr)) {
DCHECK(delegate_ptr_.is_bound());
Shell::Get()->AddPreTargetHandler(this,
ui::EventTarget::Priority::kAccessibility);
}
SwitchAccessEventHandler::~SwitchAccessEventHandler() {
Shell::Get()->RemovePreTargetHandler(this);
}
void SwitchAccessEventHandler::FlushMojoForTest() {
delegate_ptr_.FlushForTesting();
}
void SwitchAccessEventHandler::OnKeyEvent(ui::KeyEvent* event) {
DCHECK(IsSwitchAccessEnabled());
DCHECK(event);
if (ShouldForwardEvent(*event)) {
CancelEvent(event);
delegate_ptr_->DispatchKeyEvent(ui::Event::Clone(*event));
}
}
bool SwitchAccessEventHandler::ShouldForwardEvent(
const ui::KeyEvent& event) const {
// Ignore virtual key events so users can type with the onscreen keyboard.
if (ignore_virtual_key_events_ && !event.HasNativeEvent())
return false;
if (forward_key_events_)
return true;
return keys_to_capture_.find(event.key_code()) != keys_to_capture_.end();
}
} // namespace ash