keyboard_event_counter.cc 960 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2013 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_event_counter.h"
  5. #include "base/check_op.h"
  6. namespace ui {
  7. KeyboardEventCounter::KeyboardEventCounter() : total_key_presses_(0) {}
  8. KeyboardEventCounter::~KeyboardEventCounter() = default;
  9. void KeyboardEventCounter::OnKeyboardEvent(EventType event,
  10. KeyboardCode key_code) {
  11. // Updates the pressed keys and the total count of key presses.
  12. if (event == ET_KEY_PRESSED) {
  13. if (pressed_keys_.find(key_code) != pressed_keys_.end())
  14. return;
  15. pressed_keys_.insert(key_code);
  16. ++total_key_presses_;
  17. } else {
  18. DCHECK_EQ(ET_KEY_RELEASED, event);
  19. pressed_keys_.erase(key_code);
  20. }
  21. }
  22. uint32_t KeyboardEventCounter::GetKeyPressCount() const {
  23. return total_key_presses_.load();
  24. }
  25. } // namespace ui