1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_EVENTS_EVENT_H_
6 #define UI_EVENTS_EVENT_H_
13 #include "base/containers/flat_map.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/raw_ptr.h"
16 #include "base/time/time.h"
17 #include "ui/events/event_constants.h"
18 #include "ui/events/gesture_event_details.h"
19 #include "ui/events/gestures/gesture_types.h"
20 #include "ui/events/keycodes/dom/dom_key.h"
21 #include "ui/events/keycodes/keyboard_codes.h"
22 #include "ui/events/platform_event.h"
23 #include "ui/events/pointer_details.h"
24 #include "ui/events/types/event_type.h"
25 #include "ui/gfx/geometry/point.h"
26 #include "ui/gfx/geometry/point_conversions.h"
27 #include "ui/latency/latency_info.h"
35 class CancelModeEvent;
41 class MouseWheelEvent;
47 // Note: In order for Clone() to work properly, every concrete class
48 // transitively inheriting Event must implement Clone() explicitly, even if any
49 // ancestors have provided an implementation.
50 class EVENTS_EXPORT Event {
52 using Properties = base::flat_map<std::string, std::vector<uint8_t>>;
58 explicit DispatcherApi(Event* event) : event_(event) {}
60 DispatcherApi(const DispatcherApi&) = delete;
61 DispatcherApi& operator=(const DispatcherApi&) = delete;
63 void set_target(EventTarget* target) { event_->target_ = target; }
65 void set_phase(EventPhase phase) { event_->phase_ = phase; }
66 void set_result(int result) {
67 event_->result_ = static_cast<EventResult>(result);
69 void set_time_stamp(base::TimeTicks time) { event_->time_stamp_ = time; }
72 raw_ptr<Event, DanglingUntriaged> event_;
75 void SetNativeEvent(const PlatformEvent& event);
76 const PlatformEvent& native_event() const { return native_event_; }
77 EventType type() const { return type_; }
78 // time_stamp represents time since machine was booted.
79 const base::TimeTicks time_stamp() const { return time_stamp_; }
80 int flags() const { return flags_; }
82 // Returns a name for the event, typically used in logging/debugging. This is
83 // a convenience for EventTypeName(type()) (EventTypeName() is in
85 const char* GetName() const;
87 // This is only intended to be used externally by classes that are modifying
88 // events in an EventRewriter.
89 void set_flags(int flags) { flags_ = flags; }
91 EventTarget* target() const { return target_; }
92 EventPhase phase() const { return phase_; }
93 EventResult result() const { return result_; }
95 LatencyInfo* latency() { return &latency_; }
96 const LatencyInfo* latency() const { return &latency_; }
97 void set_latency(const LatencyInfo& latency) { latency_ = latency; }
99 int source_device_id() const { return source_device_id_; }
100 void set_source_device_id(int id) { source_device_id_ = id; }
102 // Sets the properties associated with this Event.
103 void SetProperties(const Properties& properties);
105 // Returns the properties associated with this event, which may be null.
106 // The properties are meant to provide a way to associate arbitrary key/value
107 // pairs with Events and not used by Event.
108 const Properties* properties() const { return properties_.get(); }
110 // By default, events are "cancelable", this means any default processing that
111 // the containing abstraction layer may perform can be prevented by calling
112 // SetHandled(). SetHandled() or StopPropagation() must not be called for
113 // events that are not cancelable.
114 bool cancelable() const { return cancelable_; }
116 // The following methods return true if the respective keys were pressed at
117 // the time the event was created.
118 bool IsShiftDown() const { return (flags_ & EF_SHIFT_DOWN) != 0; }
119 bool IsControlDown() const { return (flags_ & EF_CONTROL_DOWN) != 0; }
120 bool IsAltDown() const { return (flags_ & EF_ALT_DOWN) != 0; }
121 bool IsCommandDown() const { return (flags_ & EF_COMMAND_DOWN) != 0; }
122 bool IsAltGrDown() const { return (flags_ & EF_ALTGR_DOWN) != 0; }
123 bool IsCapsLockOn() const { return (flags_ & EF_CAPS_LOCK_ON) != 0; }
125 bool IsSynthesized() const { return (flags_ & EF_IS_SYNTHESIZED) != 0; }
127 bool IsCancelModeEvent() const { return type_ == ET_CANCEL_MODE; }
129 bool IsKeyEvent() const {
130 return type_ == ET_KEY_PRESSED || type_ == ET_KEY_RELEASED;
133 bool IsMouseEvent() const {
134 return type_ == ET_MOUSE_PRESSED ||
135 type_ == ET_MOUSE_DRAGGED ||
136 type_ == ET_MOUSE_RELEASED ||
137 type_ == ET_MOUSE_MOVED ||
138 type_ == ET_MOUSE_ENTERED ||
139 type_ == ET_MOUSE_EXITED ||
140 type_ == ET_MOUSEWHEEL ||
141 type_ == ET_MOUSE_CAPTURE_CHANGED;
144 bool IsTouchEvent() const {
145 return type_ == ET_TOUCH_RELEASED ||
146 type_ == ET_TOUCH_PRESSED ||
147 type_ == ET_TOUCH_MOVED ||
148 type_ == ET_TOUCH_CANCELLED;
151 bool IsGestureEvent() const {
153 case ET_GESTURE_SCROLL_BEGIN:
154 case ET_GESTURE_SCROLL_END:
155 case ET_GESTURE_SCROLL_UPDATE:
157 case ET_GESTURE_DOUBLE_TAP:
158 case ET_GESTURE_TAP_CANCEL:
159 case ET_GESTURE_TAP_DOWN:
160 case ET_GESTURE_TAP_UNCONFIRMED:
161 case ET_GESTURE_BEGIN:
163 case ET_GESTURE_TWO_FINGER_TAP:
164 case ET_GESTURE_PINCH_BEGIN:
165 case ET_GESTURE_PINCH_END:
166 case ET_GESTURE_PINCH_UPDATE:
167 case ET_GESTURE_LONG_PRESS:
168 case ET_GESTURE_LONG_TAP:
169 case ET_GESTURE_SWIPE:
170 case ET_GESTURE_SHOW_PRESS:
171 // When adding a gesture event which is paired with an event which
172 // occurs earlier, add the event to |IsEndingEvent|.
175 case ET_SCROLL_FLING_CANCEL:
176 case ET_SCROLL_FLING_START:
177 // These can be ScrollEvents too. EF_FROM_TOUCH determines if they're
178 // Gesture or Scroll events.
179 return (flags_ & EF_FROM_TOUCH) == EF_FROM_TOUCH;
187 // An ending event is paired with the event which started it. Setting capture
188 // should not prevent ending events from getting to their initial target.
189 bool IsEndingEvent() const {
191 case ET_TOUCH_CANCELLED:
192 case ET_GESTURE_TAP_CANCEL:
194 case ET_GESTURE_SCROLL_END:
195 case ET_GESTURE_PINCH_END:
202 bool IsScrollEvent() const {
203 // Flings can be GestureEvents too. EF_FROM_TOUCH determines if they're
204 // Gesture or Scroll events.
205 return type_ == ET_SCROLL || ((type_ == ET_SCROLL_FLING_START ||
206 type_ == ET_SCROLL_FLING_CANCEL) &&
207 !(flags() & EF_FROM_TOUCH));
210 bool IsPinchEvent() const {
211 return type_ == ET_GESTURE_PINCH_BEGIN ||
212 type_ == ET_GESTURE_PINCH_UPDATE || type_ == ET_GESTURE_PINCH_END;
215 bool IsScrollGestureEvent() const {
216 return type_ == ET_GESTURE_SCROLL_BEGIN ||
217 type_ == ET_GESTURE_SCROLL_UPDATE || type_ == ET_GESTURE_SCROLL_END;
220 bool IsFlingScrollEvent() const {
221 return type_ == ET_SCROLL_FLING_CANCEL || type_ == ET_SCROLL_FLING_START;
224 bool IsMouseWheelEvent() const { return type_ == ET_MOUSEWHEEL; }
226 bool IsLocatedEvent() const {
227 return IsMouseEvent() || IsScrollEvent() || IsTouchEvent() ||
228 IsGestureEvent() || type_ == ET_DROP_TARGET_EVENT;
231 // Convenience methods to cast |this| to a CancelModeEvent.
232 // IsCancelModeEvent() must be true as a precondition to calling these
234 CancelModeEvent* AsCancelModeEvent();
235 const CancelModeEvent* AsCancelModeEvent() const;
237 // Convenience methods to cast |this| to a GestureEvent. IsGestureEvent()
238 // must be true as a precondition to calling these methods.
239 GestureEvent* AsGestureEvent();
240 const GestureEvent* AsGestureEvent() const;
242 // Convenience methods to cast |this| to a KeyEvent. IsKeyEvent()
243 // must be true as a precondition to calling these methods.
244 KeyEvent* AsKeyEvent();
245 const KeyEvent* AsKeyEvent() const;
247 // Convenience methods to cast |this| to a LocatedEvent. IsLocatedEvent()
248 // must be true as a precondition to calling these methods.
249 LocatedEvent* AsLocatedEvent();
250 const LocatedEvent* AsLocatedEvent() const;
252 // Convenience methods to cast |this| to a MouseEvent. IsMouseEvent()
253 // must be true as a precondition to calling these methods.
254 MouseEvent* AsMouseEvent();
255 const MouseEvent* AsMouseEvent() const;
257 // Convenience methods to cast |this| to a MouseWheelEvent.
258 // IsMouseWheelEvent() must be true as a precondition to calling these
260 MouseWheelEvent* AsMouseWheelEvent();
261 const MouseWheelEvent* AsMouseWheelEvent() const;
263 // Convenience methods to cast |this| to a ScrollEvent. IsScrollEvent()
264 // must be true as a precondition to calling these methods.
265 ScrollEvent* AsScrollEvent();
266 const ScrollEvent* AsScrollEvent() const;
268 // Convenience methods to cast |this| to a TouchEvent. IsTouchEvent()
269 // must be true as a precondition to calling these methods.
270 TouchEvent* AsTouchEvent();
271 const TouchEvent* AsTouchEvent() const;
273 // Returns true if the event has a valid |native_event_|.
274 bool HasNativeEvent() const;
276 // Immediately stops the propagation of the event. This must be called only
277 // from an EventHandler during an event-dispatch. Any event handler that may
278 // be in the list will not receive the event after this is called.
279 // Note that StopPropagation() can be called only for cancelable events.
280 void StopPropagation();
281 bool stopped_propagation() const { return !!(result_ & ER_CONSUMED); }
283 // Marks the event as having been handled. A handled event does not reach the
284 // next event phase. For example, if an event is handled during the pre-target
285 // phase, then the event is dispatched to all pre-target handlers, but not to
286 // the target or post-target handlers.
287 // Note that SetHandled() can be called only for cancelable events.
289 bool handled() const { return result_ != ER_UNHANDLED; }
291 // For debugging. Not a stable serialization format.
292 virtual std::string ToString() const;
294 // Copies an arbitrary event. If you have a typed event (e.g. a MouseEvent)
295 // just use its copy constructor.
297 // Every concrete class transitively inheriting Event must implement this
298 // method, even if any ancestors have provided an implementation.
299 virtual std::unique_ptr<Event> Clone() const = 0;
302 Event(EventType type, base::TimeTicks time_stamp, int flags);
303 Event(const PlatformEvent& native_event, EventType type, int flags);
304 Event(const Event& copy);
305 Event& operator=(const Event& rhs);
307 void SetType(EventType type);
308 void set_cancelable(bool cancelable) { cancelable_ = cancelable; }
310 void set_time_stamp(base::TimeTicks time_stamp) { time_stamp_ = time_stamp; }
313 friend class EventTestApi;
316 base::TimeTicks time_stamp_;
317 LatencyInfo latency_;
319 PlatformEvent native_event_;
320 bool delete_native_event_ = false;
321 bool cancelable_ = true;
322 // Neither Event copy constructor nor the assignment operator copies
323 // `target_`, as `target_` should be explicitly set so the setter will be
324 // responsible for tracking it.
326 // TODO(crbug.com/1298696): Breaks events_unittests.
327 raw_ptr<EventTarget, DanglingUntriagedDegradeToNoOpWhenMTE> target_ = nullptr;
328 EventPhase phase_ = EP_PREDISPATCH;
329 EventResult result_ = ER_UNHANDLED;
331 // The device id the event came from, or ED_UNKNOWN_DEVICE if the information
333 int source_device_id_ = ED_UNKNOWN_DEVICE;
335 std::unique_ptr<Properties> properties_;
338 class EVENTS_EXPORT CancelModeEvent : public Event {
341 ~CancelModeEvent() override;
344 std::unique_ptr<Event> Clone() const override;
347 class EVENTS_EXPORT LocatedEvent : public Event {
349 // Convenience function that casts |event| to a LocatedEvent if it is one,
350 // otherwise returns null.
351 static const LocatedEvent* FromIfValid(const Event* event) {
352 return event && event->IsLocatedEvent() ? event->AsLocatedEvent() : nullptr;
355 ~LocatedEvent() override;
357 float x() const { return location_.x(); }
358 float y() const { return location_.y(); }
359 void set_location(const gfx::Point& location) {
360 location_ = gfx::PointF(location);
362 void set_location_f(const gfx::PointF& location) { location_ = location; }
363 gfx::Point location() const { return gfx::ToFlooredPoint(location_); }
364 const gfx::PointF& location_f() const { return location_; }
365 void set_root_location(const gfx::Point& root_location) {
366 root_location_ = gfx::PointF(root_location);
368 void set_root_location_f(const gfx::PointF& root_location) {
369 root_location_ = root_location;
371 gfx::Point root_location() const {
372 return gfx::ToFlooredPoint(root_location_);
374 const gfx::PointF& root_location_f() const { return root_location_; }
376 // Transform the locations using |inverted_root_transform| and
377 // |inverted_local_transform|. |inverted_local_transform| is only used if
378 // the event has a target.
379 virtual void UpdateForRootTransform(
380 const gfx::Transform& inverted_root_transform,
381 const gfx::Transform& inverted_local_transform);
384 void ConvertLocationToTarget(const T* source, const T* target) {
385 if (!target || target == source)
387 gfx::Point offset = gfx::ToFlooredPoint(location_);
388 T::ConvertPointToTarget(source, target, &offset);
389 gfx::Vector2d diff = gfx::ToFlooredPoint(location_) - offset;
390 location_ = location_ - diff;
394 std::string ToString() const override;
397 friend class LocatedEventTestApi;
399 LocatedEvent(const LocatedEvent& copy);
401 explicit LocatedEvent(const PlatformEvent& native_event);
403 // Create a new LocatedEvent which is identical to the provided model.
404 // If source / target windows are provided, the model location will be
405 // converted from |source| coordinate system to |target| coordinate system.
407 LocatedEvent(const LocatedEvent& model, T* source, T* target)
409 location_(model.location_),
410 root_location_(model.root_location_) {
411 ConvertLocationToTarget(source, target);
414 // Used for synthetic events in testing.
415 LocatedEvent(EventType type,
416 const gfx::PointF& location,
417 const gfx::PointF& root_location,
418 base::TimeTicks time_stamp,
421 // Location of the event relative to the target window and in the target
422 // window's coordinate space. If there is no target this is the same as
423 // |root_location_|. Native events may generate float values with sub-pixel
425 gfx::PointF location_;
427 // Location of the event. What coordinate system this is in depends upon the
428 // phase of event dispatch. For client code (meaning EventHandlers) it is
429 // generally in screen coordinates, but early on it may be in pixels and
430 // relative to a display. Native events may generate float values with
431 // sub-pixel precision.
432 gfx::PointF root_location_;
435 class EVENTS_EXPORT MouseEvent : public LocatedEvent {
437 // NOTE: On some platforms this will allow an event to be constructed from a
438 // void*, see PlatformEvent.
439 explicit MouseEvent(const PlatformEvent& native_event);
441 // Create a new MouseEvent based on the provided model.
442 // Uses the provided |type| and |flags| for the new event.
443 // If source / target windows are provided, the model location will be
444 // converted from |source| coordinate system to |target| coordinate system.
446 MouseEvent(const MouseEvent& model, T* source, T* target)
447 : LocatedEvent(model, source, target),
448 changed_button_flags_(model.changed_button_flags_),
449 movement_(model.movement_),
450 pointer_details_(model.pointer_details_) {}
453 MouseEvent(const MouseEvent& model,
458 : LocatedEvent(model, source, target),
459 changed_button_flags_(model.changed_button_flags_),
460 movement_(model.movement_),
461 pointer_details_(model.pointer_details_) {
466 // Note: Use the ctor for MouseWheelEvent if type is ET_MOUSEWHEEL.
467 MouseEvent(EventType type,
468 const gfx::PointF& location,
469 const gfx::PointF& root_location,
470 base::TimeTicks time_stamp,
472 int changed_button_flags,
473 const PointerDetails& pointer_details =
474 PointerDetails(EventPointerType::kMouse, kPointerIdMouse));
476 // DEPRECATED: Prefer constructor that takes gfx::PointF.
477 MouseEvent(EventType type,
478 const gfx::Point& location,
479 const gfx::Point& root_location,
480 base::TimeTicks time_stamp,
482 int changed_button_flags,
483 const PointerDetails& pointer_details =
484 PointerDetails(EventPointerType::kMouse, kPointerIdMouse));
486 MouseEvent(const MouseEvent& copy);
487 ~MouseEvent() override;
489 void InitializeNative();
491 class DispatcherApi {
493 explicit DispatcherApi(MouseEvent* event) : event_(event) {}
495 DispatcherApi(const DispatcherApi&) = delete;
496 DispatcherApi& operator=(const DispatcherApi&) = delete;
498 // TODO(eirage): convert this to builder pattern.
499 void set_movement(const gfx::Vector2dF& movement) {
500 event_->movement_ = movement;
501 event_->set_flags(event_->flags() | EF_UNADJUSTED_MOUSE);
505 raw_ptr<MouseEvent> event_;
508 // Conveniences to quickly test what button is down
509 bool IsOnlyLeftMouseButton() const {
510 return button_flags() == EF_LEFT_MOUSE_BUTTON;
513 bool IsLeftMouseButton() const {
514 return (flags() & EF_LEFT_MOUSE_BUTTON) != 0;
517 bool IsOnlyMiddleMouseButton() const {
518 return button_flags() == EF_MIDDLE_MOUSE_BUTTON;
521 bool IsMiddleMouseButton() const {
522 return (flags() & EF_MIDDLE_MOUSE_BUTTON) != 0;
525 bool IsOnlyRightMouseButton() const {
526 return button_flags() == EF_RIGHT_MOUSE_BUTTON;
529 bool IsRightMouseButton() const {
530 return (flags() & EF_RIGHT_MOUSE_BUTTON) != 0;
533 bool IsAnyButton() const { return button_flags() != 0; }
535 // Returns the flags for the mouse buttons.
536 int button_flags() const {
537 return flags() & (EF_LEFT_MOUSE_BUTTON | EF_MIDDLE_MOUSE_BUTTON |
538 EF_RIGHT_MOUSE_BUTTON | EF_BACK_MOUSE_BUTTON |
539 EF_FORWARD_MOUSE_BUTTON);
542 // Compares two mouse down events and returns true if the second one should
543 // be considered a repeat of the first.
544 static bool IsRepeatedClickEvent(const MouseEvent& event1,
545 const MouseEvent& event2);
547 // Get the click count. Can be 1, 2 or 3 for mousedown messages, 0 otherwise.
548 int GetClickCount() const;
550 // Set the click count for a mousedown message. Can be 1, 2 or 3.
551 void SetClickCount(int click_count);
553 // Identifies the button that changed. During a press this corresponds to the
554 // button that was pressed and during a release this corresponds to the button
555 // that was released.
556 // NOTE: during a press and release flags() contains the complete set of
557 // flags. Use this to determine the button that was pressed or released.
558 int changed_button_flags() const { return changed_button_flags_; }
560 // Updates the button that changed.
561 void set_changed_button_flags(int flags) { changed_button_flags_ = flags; }
563 const gfx::Vector2dF& movement() const { return movement_; }
565 const PointerDetails& pointer_details() const { return pointer_details_; }
568 std::string ToString() const override;
569 std::unique_ptr<Event> Clone() const override;
571 // Resets the last_click_event_ for unit tests.
572 static void ResetLastClickForTest();
575 FRIEND_TEST_ALL_PREFIXES(EventTest, DoubleClickRequiresUniqueTimestamp);
576 FRIEND_TEST_ALL_PREFIXES(EventTest, SingleClickRightLeft);
578 // Returns the repeat count based on the previous mouse click, if it is
579 // recent enough and within a small enough distance.
580 static int GetRepeatCount(const MouseEvent& click_event);
582 // See description above getter for details.
583 int changed_button_flags_;
585 // Raw mouse movement value reported from mouse hardware. The value of this is
586 // platform dependent and may change depending upon the hardware connected to
587 // the device. This field is only set if the flag EF_UNADJUSTED_MOUSE is
588 // present (which is the case on windows platforms, and CrOS if the
589 // kEnableOrdinalMotion flag is set).
591 // TODO(b/171249701): always enable on CrOS.
592 gfx::Vector2dF movement_;
594 // The most recent user-generated MouseEvent, used to detect double clicks.
595 static MouseEvent* last_click_event_;
597 // Structure for holding pointer details for implementing PointerEvents API.
598 PointerDetails pointer_details_;
603 class EVENTS_EXPORT MouseWheelEvent : public MouseEvent {
605 // See |offset| for details.
606 static const int kWheelDelta;
608 explicit MouseWheelEvent(const PlatformEvent& native_event);
609 explicit MouseWheelEvent(const ScrollEvent& scroll_event);
610 MouseWheelEvent(const MouseEvent& mouse_event, int x_offset, int y_offset);
611 MouseWheelEvent(const MouseWheelEvent& copy);
612 ~MouseWheelEvent() override;
615 MouseWheelEvent(const MouseWheelEvent& model, T* source, T* target)
616 : MouseEvent(model, source, target, model.type(), model.flags()),
617 offset_(model.x_offset(), model.y_offset()),
618 tick_120ths_(model.tick_120ths()) {}
620 // Used for synthetic events in testing and by the gesture recognizer.
622 const gfx::Vector2d& offset,
623 const gfx::PointF& location,
624 const gfx::PointF& root_location,
625 base::TimeTicks time_stamp,
627 int changed_button_flags,
628 const absl::optional<gfx::Vector2d> tick_120ths = absl::nullopt);
630 // DEPRECATED: Prefer the constructor that takes gfx::PointF.
631 MouseWheelEvent(const gfx::Vector2d& offset,
632 const gfx::Point& location,
633 const gfx::Point& root_location,
634 base::TimeTicks time_stamp,
636 int changed_button_flags);
638 // The amount to scroll. This is not necessarily linearly related to the
639 // amount that the wheel moved, due to scroll acceleration.
640 // Note: x_offset() > 0/y_offset() > 0 means scroll left/up.
641 int x_offset() const { return offset_.x(); }
642 int y_offset() const { return offset_.y(); }
643 const gfx::Vector2d& offset() const { return offset_; }
645 // The amount the wheel(s) moved, in 120ths of a tick.
646 const gfx::Vector2d& tick_120ths() const { return tick_120ths_; }
649 std::unique_ptr<Event> Clone() const override;
652 gfx::Vector2d offset_;
653 gfx::Vector2d tick_120ths_;
656 // NOTE: Pen (stylus) events use TouchEvent with kPen. They were
657 // originally implemented as MouseEvent but switched to TouchEvent when UX
658 // decided not to show hover effects for pen.
659 class EVENTS_EXPORT TouchEvent : public LocatedEvent {
661 explicit TouchEvent(const PlatformEvent& native_event);
663 // Create a new TouchEvent which is identical to the provided model.
664 // If source / target windows are provided, the model location will be
665 // converted from |source| coordinate system to |target| coordinate system.
667 TouchEvent(const TouchEvent& model, T* source, T* target)
668 : LocatedEvent(model, source, target),
669 unique_event_id_(model.unique_event_id_),
670 may_cause_scrolling_(model.may_cause_scrolling_),
672 pointer_details_(model.pointer_details_) {}
674 TouchEvent(EventType type,
675 const gfx::PointF& location,
676 const gfx::PointF& root_location,
677 base::TimeTicks time_stamp,
678 const PointerDetails& pointer_details,
681 // DEPRECATED: Prefer the constructor that takes gfx::PointF.
682 TouchEvent(EventType type,
683 const gfx::Point& location,
684 base::TimeTicks time_stamp,
685 const PointerDetails& pointer_details,
688 TouchEvent(const TouchEvent& copy);
690 ~TouchEvent() override;
692 // A unique identifier for this event.
693 uint32_t unique_event_id() const { return unique_event_id_; }
695 void set_may_cause_scrolling(bool causes) { may_cause_scrolling_ = causes; }
696 bool may_cause_scrolling() const { return may_cause_scrolling_; }
698 void set_hovering(bool hovering) { hovering_ = hovering; }
699 bool hovering() const { return hovering_; }
701 // Overridden from LocatedEvent.
702 void UpdateForRootTransform(
703 const gfx::Transform& inverted_root_transform,
704 const gfx::Transform& inverted_local_transform) override;
706 // Marks the event as not participating in synchronous gesture recognition.
707 void DisableSynchronousHandling();
708 bool synchronous_handling_disabled() const {
709 return !!(result() & ER_DISABLE_SYNC_HANDLING);
712 const PointerDetails& pointer_details() const { return pointer_details_; }
713 void SetPointerDetailsForTest(const PointerDetails& pointer_details);
715 float ComputeRotationAngle() const;
718 std::unique_ptr<Event> Clone() const override;
721 // A unique identifier for the touch event.
722 // NOTE: this is *not* serialized over mojom, as the number is unique to
723 // a particular process, and as mojom may go cross process, to serialize could
724 // lead to conflicts.
725 uint32_t unique_event_id_;
727 // Whether the (unhandled) touch event will produce a scroll event (e.g., a
728 // touchmove that exceeds the platform slop region, or a touchend that
729 // causes a fling). Defaults to false.
730 bool may_cause_scrolling_ = false;
732 // True for devices like some pens when they support hovering over
733 // digitizer and they send events while hovering.
734 bool hovering_ = false;
736 // Structure for holding pointer details for implementing PointerEvents API.
737 PointerDetails pointer_details_;
740 // A KeyEvent is really two distinct classes, melded together due to the
741 // DOM legacy of Windows key events: a keystroke event (is_char_ == false),
742 // or a character event (is_char_ == true).
744 // For a keystroke event,
745 // -- |bool is_char_| is false.
746 // -- |EventType Event::type()| can be ET_KEY_PRESSED or ET_KEY_RELEASED.
747 // -- |DomCode code_| and |int Event::flags()| represent the physical key event.
748 // - code_ is a platform-independent representation of the physical key,
749 // based on DOM UI Events KeyboardEvent |code| values. It does not
750 // vary depending on key layout.
751 // http://www.w3.org/TR/DOM-Level-3-Events-code/
752 // - Event::flags() provides the active modifiers for the physical key
753 // press. Its value reflects the state after the event; that is, for
754 // a modifier key, a press includes the corresponding flag and a release
756 // -- |DomKey key_| provides the meaning (character or action) of the key
757 // event, in the context of the active layout and modifiers. It corresponds
758 // to DOM UI Events KeyboardEvent |key| values.
759 // http://www.w3.org/TR/DOM-Level-3-Events-key/
760 // -- |KeyboardCode key_code_| supports the legacy web event |keyCode| field,
761 // and its VKEY_ values are chosen to match Windows/IE for compatibility.
762 // For printable characters, this may or may not be a layout-mapped value,
763 // imitating MS Windows: if the mapped key generates a character that has
764 // an associated VKEY_ code, then key_code_ is that code; if not, then
765 // key_code_ is the unmapped VKEY_ code. For example, US, Greek, Cyrillic,
766 // Japanese, etc. all use VKEY_Q for the key beside Tab, while French uses
767 // VKEY_A. The stored key_code_ is non-located (e.g. VKEY_SHIFT rather than
768 // VKEY_LSHIFT, VKEY_1 rather than VKEY_NUMPAD1).
769 // -- |uint32_t scan_code_| [USE_OZONE only] supports remapping of the top
770 // function row based on a sysfs attribute provided by the kernel. This
771 // allows devices to have a custom top row layout and still be able to
772 // perform translation back and forth between F-Key and Action-Key. The
773 // value of this scan code is device specific.
775 // For a character event,
776 // -- |bool is_char_| is true.
777 // -- |EventType Event::type()| is ET_KEY_PRESSED.
778 // -- |DomCode code_| is DomCode::NONE.
779 // -- |DomKey key_| is a UTF-16 code point.
780 // -- |KeyboardCode key_code_| is conflated with the character-valued key_
781 // by some code, because both arrive in the wParam field of a Windows event.
783 class EVENTS_EXPORT KeyEvent : public Event {
785 // Create a KeyEvent from a NativeEvent. For Windows this native event can
786 // be either a keystroke message (WM_KEYUP/WM_KEYDOWN) or a character message
787 // (WM_CHAR). Other systems have only keystroke events.
788 explicit KeyEvent(const PlatformEvent& native_event);
790 // Create a KeyEvent from a NativeEvent but with mocked flags.
791 // This method is necessary for Windows since MSG does not contain all flags.
792 KeyEvent(const PlatformEvent& native_event, int event_flags);
794 // Create a keystroke event from a legacy KeyboardCode.
795 // This should not be used in new code.
796 KeyEvent(EventType type,
797 KeyboardCode key_code,
799 base::TimeTicks time_stamp = base::TimeTicks());
801 // Create a fully defined keystroke event.
802 KeyEvent(EventType type,
803 KeyboardCode key_code,
807 base::TimeTicks time_stamp,
808 bool is_char = false);
810 // Create a character event.
811 KeyEvent(char16_t character,
812 KeyboardCode key_code,
815 base::TimeTicks time_stamp = base::TimeTicks());
817 // Used for synthetic events with code of DOM KeyboardEvent (e.g. 'KeyA')
818 // See also: ui/events/keycodes/dom/dom_values.txt
819 KeyEvent(EventType type, KeyboardCode key_code, DomCode code, int flags);
821 KeyEvent(const KeyEvent& rhs);
823 KeyEvent& operator=(const KeyEvent& rhs);
825 ~KeyEvent() override;
827 // Returns true if synthesizing key repeat in InitializeNative is enabled.
828 static bool IsSynthesizeKeyRepeatEnabled();
830 // Sets whether to enable synthesizing key repeat in InitializeNative().
831 static void SetSynthesizeKeyRepeatEnabled(bool enabled);
833 void InitializeNative();
835 // This bypasses the normal mapping from keystroke events to characters,
836 // which allows an I18N virtual keyboard to fabricate a keyboard event that
837 // does not have a corresponding KeyboardCode (example: U+00E1 Latin small
838 // letter A with acute, U+0410 Cyrillic capital letter A).
839 void set_character(char16_t character) {
840 key_ = DomKey::FromCharacter(character);
843 // Gets the character generated by this key event. It only supports Unicode
845 char16_t GetCharacter() const;
847 // If this is a keystroke event with key_code_ VKEY_RETURN, returns '\r';
848 // otherwise returns the same as GetCharacter().
849 char16_t GetUnmodifiedText() const;
851 // If the Control key is down in the event, returns a layout-independent
852 // character (corresponding to US layout); otherwise returns the same
853 // as GetUnmodifiedText().
854 char16_t GetText() const;
856 // True if this is a character event, false if this is a keystroke event.
857 bool is_char() const { return is_char_; }
859 bool is_repeat() const { return (flags() & EF_IS_REPEAT) != 0; }
861 // Gets the associated (Windows-based) KeyboardCode for this key event.
862 // Historically, this has also been used to obtain the character associated
863 // with a character event, because both use the Window message 'wParam' field.
864 // This should be avoided; if necessary for backwards compatibility, use
865 // GetConflatedWindowsKeyCode().
866 KeyboardCode key_code() const { return key_code_; }
868 // This is only intended to be used externally by classes that are modifying
869 // events in an EventRewriter.
870 void set_key_code(KeyboardCode key_code) { key_code_ = key_code; }
872 #if defined(USE_OZONE)
873 // The scan code of the physical key. This is used to perform the mapping
874 // of top row keys from Actions back to F-Keys on new Chrome OS keyboards
875 // that supply the mapping via the kernel.
876 uint32_t scan_code() const { return scan_code_; }
877 void set_scan_code(uint32_t scan_code) { scan_code_ = scan_code; }
878 #endif // defined(USE_OZONE)
880 // Returns the same value as key_code(), except that located codes are
881 // returned in place of non-located ones (e.g. VKEY_LSHIFT or VKEY_RSHIFT
882 // instead of VKEY_SHIFT). This is a hybrid of semantic and physical
883 // for legacy DOM reasons.
884 KeyboardCode GetLocatedWindowsKeyboardCode() const;
886 // For a keystroke event, returns the same value as key_code().
887 // For a character event, returns the same value as GetCharacter().
888 // This exists for backwards compatibility with Windows key events.
889 uint16_t GetConflatedWindowsKeyCode() const;
891 // Returns true for [Alt]+<num-pad digit> Unicode alt key codes used by Win.
892 // TODO(msw): Additional work may be needed for analogues on other platforms.
893 bool IsUnicodeKeyCode() const;
895 // Returns the DOM .code (physical key identifier) for a keystroke event.
896 DomCode code() const { return code_; }
897 std::string GetCodeString() const;
899 // Returns the DOM .key (layout meaning) for a keystroke event.
900 DomKey GetDomKey() const;
902 // Normalizes flags_ so that it describes the state after the event.
903 // (Native X11 event flags describe the state before the event.)
904 void NormalizeFlags();
907 std::string ToString() const override;
908 std::unique_ptr<Event> Clone() const override;
911 friend class KeyEventTestApi;
913 // This allows a subclass TranslatedKeyEvent to be a non character event.
914 void set_is_char(bool is_char) { is_char_ = is_char; }
917 // Determine key_ on a keystroke event from code_ and flags().
918 void ApplyLayout() const;
920 // Tells if this is a repeated KeyEvent based on |last_key_event|, which is
921 // then updated with the new last KeyEvent address.
922 bool IsRepeated(KeyEvent** last_key_event);
924 // Returns the pointer for the last processed KeyEvent.
925 KeyEvent** GetLastKeyEvent();
927 KeyboardCode key_code_;
929 #if defined(USE_OZONE)
930 // The scan code of the physical key on Chrome OS.
931 uint32_t scan_code_ = 0;
932 #endif // defined(USE_OZONE)
934 // DOM KeyboardEvent |code| (e.g. DomCode::US_A, DomCode::SPACE).
935 // http://www.w3.org/TR/DOM-Level-3-Events-code/
937 // This value represents the physical position in the keyboard and can be
938 // converted from / to keyboard scan code like XKB.
941 // True if this is a character event, false if this is a keystroke event.
942 bool is_char_ = false;
944 // TODO(kpschoedel): refactor so that key_ is not mutable.
945 // This requires defining the KeyEvent completely at construction rather
946 // than lazily under GetCharacter(), which likely also means removing
947 // the two 'incomplete' constructors. crbug.com/444045
949 // DOM KeyboardEvent |key|
950 // http://www.w3.org/TR/DOM-Level-3-Events-key/
952 // This value represents the meaning of a key, which is either a Unicode
953 // character, or a named DomKey:: value.
954 // This is not necessarily initialized when the event is constructed;
955 // it may be set only if and when GetCharacter() or GetDomKey() is called.
956 mutable DomKey key_ = DomKey::NONE;
958 static KeyEvent* last_key_event_;
959 #if defined(USE_OZONE)
960 static KeyEvent* last_ibus_key_event_;
963 static bool synthesize_key_repeat_enabled_;
966 class EVENTS_EXPORT ScrollEvent : public MouseEvent {
968 explicit ScrollEvent(const PlatformEvent& native_event);
971 ScrollEvent(const ScrollEvent& model, T* source, T* target)
972 : MouseEvent(model, source, target),
973 x_offset_(model.x_offset_),
974 y_offset_(model.y_offset_),
975 x_offset_ordinal_(model.x_offset_ordinal_),
976 y_offset_ordinal_(model.y_offset_ordinal_),
977 finger_count_(model.finger_count_),
978 momentum_phase_(model.momentum_phase_),
979 scroll_event_phase_(model.scroll_event_phase_) {}
981 ScrollEvent(EventType type,
982 const gfx::PointF& location,
983 const gfx::PointF& root_location,
984 base::TimeTicks time_stamp,
988 float x_offset_ordinal,
989 float y_offset_ordinal,
991 EventMomentumPhase momentum_phase = EventMomentumPhase::NONE,
992 ScrollEventPhase phase = ScrollEventPhase::kNone);
994 // DEPRECATED: Prefer the constructor that takes gfx::PointF.
995 ScrollEvent(EventType type,
996 const gfx::Point& location,
997 base::TimeTicks time_stamp,
1001 float x_offset_ordinal,
1002 float y_offset_ordinal,
1004 EventMomentumPhase momentum_phase = EventMomentumPhase::NONE,
1005 ScrollEventPhase phase = ScrollEventPhase::kNone);
1007 ScrollEvent(const ScrollEvent& copy);
1008 ~ScrollEvent() override;
1010 // Scale the scroll event's offset value.
1011 // This is useful in the multi-monitor setup where it needs to be scaled
1012 // to provide a consistent user experience.
1013 void Scale(const float factor);
1015 float x_offset() const { return x_offset_; }
1016 float y_offset() const { return y_offset_; }
1017 float x_offset_ordinal() const { return x_offset_ordinal_; }
1018 float y_offset_ordinal() const { return y_offset_ordinal_; }
1019 int finger_count() const { return finger_count_; }
1020 EventMomentumPhase momentum_phase() const { return momentum_phase_; }
1021 ScrollEventPhase scroll_event_phase() const { return scroll_event_phase_; }
1024 std::string ToString() const override;
1025 std::unique_ptr<Event> Clone() const override;
1028 // Potential accelerated offsets.
1031 // Unaccelerated offsets.
1032 float x_offset_ordinal_;
1033 float y_offset_ordinal_;
1034 // Number of fingers on the pad.
1037 // For non-fling events, provides momentum information (e.g. for the case
1038 // where the device provides continuous event updates during a fling).
1039 EventMomentumPhase momentum_phase_ = EventMomentumPhase::NONE;
1041 // Provides phase information if device can provide.
1042 ScrollEventPhase scroll_event_phase_ = ScrollEventPhase::kNone;
1045 class EVENTS_EXPORT GestureEvent : public LocatedEvent {
1047 // The constructor takes a default unique_touch_id of zero to support many
1048 // (80+) existing tests that doesn't care about this id.
1049 GestureEvent(float x,
1052 base::TimeTicks time_stamp,
1053 const GestureEventDetails& details,
1054 uint32_t unique_touch_event_id = 0);
1056 // Create a new GestureEvent which is identical to the provided model.
1057 // If source / target windows are provided, the model location will be
1058 // converted from |source| coordinate system to |target| coordinate system.
1059 template <typename T>
1060 GestureEvent(const GestureEvent& model, T* source, T* target)
1061 : LocatedEvent(model, source, target), details_(model.details_) {}
1062 GestureEvent(const GestureEvent& copy);
1063 ~GestureEvent() override;
1065 const GestureEventDetails& details() const { return details_; }
1067 uint32_t unique_touch_event_id() const { return unique_touch_event_id_; }
1070 std::string ToString() const override;
1071 std::unique_ptr<Event> Clone() const override;
1074 GestureEventDetails details_;
1076 // The unique id of the touch event that caused the gesture event to be
1077 // dispatched. This field gets a non-zero value only for gestures that are
1078 // released through TouchDispositionGestureFilter::SendGesture. The gesture
1079 // events that aren't fired directly in response to processing a touch-event
1080 // (e.g. timer fired ones), this id is zero. See crbug.com/618738.
1081 uint32_t unique_touch_event_id_;
1086 #endif // UI_EVENTS_EVENT_H_