key_accessibility_enabler.cc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2018 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.
  4. #include "ash/accessibility/chromevox/key_accessibility_enabler.h"
  5. #include "ash/accessibility/chromevox/spoken_feedback_enabler.h"
  6. #include "ash/shell.h"
  7. #include "ash/system/power/power_button_screenshot_controller.h"
  8. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  9. #include "ui/events/base_event_utils.h"
  10. #include "ui/events/event.h"
  11. namespace ash {
  12. KeyAccessibilityEnabler::KeyAccessibilityEnabler() {
  13. Shell::Get()->AddPreTargetHandler(this,
  14. ui::EventTarget::Priority::kAccessibility);
  15. }
  16. KeyAccessibilityEnabler::~KeyAccessibilityEnabler() {
  17. Shell::Get()->RemovePreTargetHandler(this);
  18. }
  19. void KeyAccessibilityEnabler::OnKeyEvent(ui::KeyEvent* event) {
  20. if ((event->type() != ui::ET_KEY_PRESSED &&
  21. event->type() != ui::ET_KEY_RELEASED) ||
  22. !Shell::Get()->tablet_mode_controller()->InTabletMode())
  23. return;
  24. if (event->key_code() == ui::VKEY_VOLUME_DOWN)
  25. vol_down_pressed_ = event->type() == ui::ET_KEY_PRESSED;
  26. else if (event->key_code() == ui::VKEY_VOLUME_UP)
  27. vol_up_pressed_ = event->type() == ui::ET_KEY_PRESSED;
  28. else
  29. other_key_pressed_ = event->type() == ui::ET_KEY_PRESSED;
  30. if (vol_down_pressed_ && vol_up_pressed_ && !other_key_pressed_) {
  31. if (!spoken_feedback_enabler_.get()) {
  32. spoken_feedback_enabler_ = std::make_unique<SpokenFeedbackEnabler>();
  33. first_time_both_volume_keys_pressed_ = ui::EventTimeForNow();
  34. }
  35. if (ui::EventTimeForNow() - first_time_both_volume_keys_pressed_ >
  36. PowerButtonScreenshotController::kScreenshotChordDelay)
  37. event->StopPropagation();
  38. } else if (spoken_feedback_enabler_.get()) {
  39. spoken_feedback_enabler_.reset();
  40. }
  41. }
  42. } // namespace ash