keyboard_event_counter.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #ifndef UI_EVENTS_KEYBOARD_EVENT_COUNTER_H_
  5. #define UI_EVENTS_KEYBOARD_EVENT_COUNTER_H_
  6. #include <stddef.h>
  7. #include <atomic>
  8. #include <set>
  9. #include "ui/events/events_export.h"
  10. #include "ui/events/keycodes/keyboard_codes.h"
  11. #include "ui/events/types/event_type.h"
  12. namespace ui {
  13. // This class tracks the total number of keypresses based on the OnKeyboardEvent
  14. // calls it receives from the client.
  15. // Multiple key down events for the same key are counted as one keypress until
  16. // the same key is released.
  17. class EVENTS_EXPORT KeyboardEventCounter {
  18. public:
  19. KeyboardEventCounter();
  20. KeyboardEventCounter(const KeyboardEventCounter&) = delete;
  21. KeyboardEventCounter& operator=(const KeyboardEventCounter&) = delete;
  22. ~KeyboardEventCounter();
  23. // Returns the total number of keypresses since its creation.
  24. // Can be called on any thread.
  25. uint32_t GetKeyPressCount() const;
  26. // The client should call this method on key down or key up events.
  27. // Must be called on a single thread.
  28. void OnKeyboardEvent(EventType event, KeyboardCode key_code);
  29. private:
  30. // The set of keys currently held down.
  31. std::set<KeyboardCode> pressed_keys_;
  32. std::atomic<uint32_t> total_key_presses_;
  33. };
  34. } // namespace ui
  35. #endif // UI_EVENTS_KEYBOARD_EVENT_COUNTER_H_