keyboard_hook_base.cc 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 "ui/events/keyboard_hook_base.h"
  5. #include <utility>
  6. #include "base/containers/contains.h"
  7. #include "ui/events/event.h"
  8. #include "ui/events/keycodes/dom/dom_code.h"
  9. namespace ui {
  10. KeyboardHookBase::KeyboardHookBase(
  11. absl::optional<base::flat_set<DomCode>> dom_codes,
  12. KeyEventCallback callback)
  13. : key_event_callback_(std::move(callback)),
  14. dom_codes_(std::move(dom_codes)) {
  15. DCHECK(key_event_callback_);
  16. }
  17. KeyboardHookBase::~KeyboardHookBase() = default;
  18. bool KeyboardHookBase::IsKeyLocked(DomCode dom_code) const {
  19. return ShouldCaptureKeyEvent(dom_code);
  20. }
  21. bool KeyboardHookBase::RegisterHook() {
  22. NOTIMPLEMENTED();
  23. return false;
  24. }
  25. bool KeyboardHookBase::ShouldCaptureKeyEvent(DomCode dom_code) const {
  26. if (dom_code == DomCode::NONE)
  27. return false;
  28. return !dom_codes_ || base::Contains(dom_codes_.value(), dom_code);
  29. }
  30. void KeyboardHookBase::ForwardCapturedKeyEvent(KeyEvent* event) {
  31. key_event_callback_.Run(event);
  32. }
  33. } // namespace ui