key_hold_detector.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2014 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 ASH_ACCELERATORS_KEY_HOLD_DETECTOR_H_
  5. #define ASH_ACCELERATORS_KEY_HOLD_DETECTOR_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ui/events/event_handler.h"
  9. namespace ui {
  10. class KeyEvent;
  11. }
  12. namespace ash {
  13. // This class is used to implement action when a user press and hold the key.
  14. // When a user just pressed and released a key, normal pressed event gets
  15. // generated upon release.
  16. class ASH_EXPORT KeyHoldDetector : public ui::EventHandler {
  17. public:
  18. class Delegate {
  19. public:
  20. virtual ~Delegate() {}
  21. // If this return false, the event handler does not process
  22. // the event at all.
  23. virtual bool ShouldProcessEvent(const ui::KeyEvent* event) const = 0;
  24. // This should return true if the event should start the state transition.
  25. virtual bool IsStartEvent(const ui::KeyEvent* event) const = 0;
  26. // Whether to stop event propagation after processing.
  27. virtual bool ShouldStopEventPropagation() const = 0;
  28. // Called when the key is held.
  29. virtual void OnKeyHold(const ui::KeyEvent* event) = 0;
  30. // Called when the key is release after hold.
  31. virtual void OnKeyUnhold(const ui::KeyEvent* event) = 0;
  32. };
  33. explicit KeyHoldDetector(std::unique_ptr<Delegate> delegate);
  34. KeyHoldDetector(const KeyHoldDetector&) = delete;
  35. KeyHoldDetector& operator=(const KeyHoldDetector&) = delete;
  36. ~KeyHoldDetector() override;
  37. // ui::EventHandler overrides:
  38. void OnKeyEvent(ui::KeyEvent* key_event) override;
  39. private:
  40. // A state to keep track of one click and click and hold operation.
  41. //
  42. // One click:
  43. // INITIAL --(first press)--> PRESSED --(release)--> INITIAL[SEND PRESS]
  44. //
  45. // Click and hold:
  46. // INITIAL --(first press)--> PRESSED --(press)-->
  47. // HOLD[OnKeyHold] --(press)--> HOLD[OnKeyHold] --(release)-->
  48. // INITIAL[OnKeyUnhold]
  49. enum State { INITIAL, PRESSED, HOLD };
  50. State state_;
  51. std::unique_ptr<Delegate> delegate_;
  52. };
  53. } // namespace ash
  54. #endif // ASH_ACCELERATORS_KEY_HOLD_DETECTOR_H_