| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #include "chrome/browser/chromeos/accessibility/switch_access_event_handler.h" | 5 #include "chrome/browser/chromeos/accessibility/switch_access_event_handler.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" | 9 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" |
| 10 #include "chrome/browser/chromeos/accessibility/event_handler_common.h" | 10 #include "chrome/browser/chromeos/accessibility/event_handler_common.h" |
| 11 #include "chrome/common/extensions/extension_constants.h" | 11 #include "chrome/common/extensions/extension_constants.h" |
| 12 #include "extensions/browser/extension_host.h" | 12 #include "extensions/browser/extension_host.h" |
| 13 #include "ui/events/event.h" | 13 #include "ui/events/event.h" |
| 14 | 14 |
| 15 namespace chromeos { | 15 namespace chromeos { |
| 16 | 16 |
| 17 SwitchAccessEventHandler::SwitchAccessEventHandler() { | 17 SwitchAccessEventHandler::SwitchAccessEventHandler() { |
| 18 if (ash::Shell::HasInstance()) | 18 if (ash::Shell::HasInstance()) |
| 19 ash::Shell::Get()->AddPreTargetHandler(this); | 19 ash::Shell::Get()->AddPreTargetHandler(this); |
| 20 } | 20 } |
| 21 | 21 |
| 22 SwitchAccessEventHandler::~SwitchAccessEventHandler() { | 22 SwitchAccessEventHandler::~SwitchAccessEventHandler() { |
| 23 if (ash::Shell::HasInstance()) | 23 if (ash::Shell::HasInstance()) |
| 24 ash::Shell::Get()->RemovePreTargetHandler(this); | 24 ash::Shell::Get()->RemovePreTargetHandler(this); |
| 25 } | 25 } |
| 26 | 26 |
| 27 void SwitchAccessEventHandler::SetKeysToCapture( |
| 28 const std::set<int>& key_codes) { |
| 29 captured_keys_ = key_codes; |
| 30 } |
| 31 |
| 27 void SwitchAccessEventHandler::OnKeyEvent(ui::KeyEvent* event) { | 32 void SwitchAccessEventHandler::OnKeyEvent(ui::KeyEvent* event) { |
| 28 DCHECK(event); | 33 DCHECK(event); |
| 29 | 34 |
| 30 ui::KeyboardCode key_code = event->key_code(); | 35 ui::KeyboardCode key_code = event->key_code(); |
| 31 if (key_code == ui::VKEY_1 || key_code == ui::VKEY_2 || | 36 if (captured_keys_.find(key_code) != captured_keys_.end()) { |
| 32 key_code == ui::VKEY_3 || key_code == ui::VKEY_4 || | |
| 33 key_code == ui::VKEY_5 || key_code == ui::VKEY_6 || | |
| 34 key_code == ui::VKEY_7 || key_code == ui::VKEY_8 || | |
| 35 key_code == ui::VKEY_9) { | |
| 36 CancelEvent(event); | 37 CancelEvent(event); |
| 37 DispatchKeyEventToSwitchAccess(*event); | 38 DispatchKeyEventToSwitchAccess(*event); |
| 38 } | 39 } |
| 39 } | 40 } |
| 40 | 41 |
| 41 void SwitchAccessEventHandler::CancelEvent(ui::Event* event) { | 42 void SwitchAccessEventHandler::CancelEvent(ui::Event* event) { |
| 42 DCHECK(event); | 43 DCHECK(event); |
| 43 if (event->cancelable()) { | 44 if (event->cancelable()) { |
| 44 event->SetHandled(); | 45 event->SetHandled(); |
| 45 event->StopPropagation(); | 46 event->StopPropagation(); |
| 46 } | 47 } |
| 47 } | 48 } |
| 48 | 49 |
| 49 void SwitchAccessEventHandler::DispatchKeyEventToSwitchAccess( | 50 void SwitchAccessEventHandler::DispatchKeyEventToSwitchAccess( |
| 50 const ui::KeyEvent& event) { | 51 const ui::KeyEvent& event) { |
| 51 extensions::ExtensionHost* host = | 52 extensions::ExtensionHost* host = |
| 52 GetAccessibilityExtensionHost(extension_misc::kSwitchAccessExtensionId); | 53 GetAccessibilityExtensionHost(extension_misc::kSwitchAccessExtensionId); |
| 53 ForwardKeyToExtension(event, host); | 54 ForwardKeyToExtension(event, host); |
| 54 } | 55 } |
| 55 | 56 |
| 56 } // namespace chromeos | 57 } // namespace chromeos |
| OLD | NEW |