[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / ui / events / event.h
blobe077ed34841def81557fac74be799e10d8d751f1
1 // Copyright (c) 2012 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.
5 #ifndef UI_EVENTS_EVENT_H_
6 #define UI_EVENTS_EVENT_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/event_types.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "ui/events/event_constants.h"
16 #include "ui/events/gesture_event_details.h"
17 #include "ui/events/gestures/gesture_types.h"
18 #include "ui/events/keycodes/dom/dom_key.h"
19 #include "ui/events/keycodes/keyboard_codes.h"
20 #include "ui/events/latency_info.h"
21 #include "ui/gfx/geometry/point.h"
22 #include "ui/gfx/geometry/point_conversions.h"
24 namespace gfx {
25 class Transform;
28 namespace ui {
29 class EventTarget;
30 enum class DomCode;
32 class EVENTS_EXPORT Event {
33 public:
34 static scoped_ptr<Event> Clone(const Event& event);
36 virtual ~Event();
38 class DispatcherApi {
39 public:
40 explicit DispatcherApi(Event* event) : event_(event) {}
42 void set_target(EventTarget* target) {
43 event_->target_ = target;
46 void set_phase(EventPhase phase) { event_->phase_ = phase; }
47 void set_result(int result) {
48 event_->result_ = static_cast<EventResult>(result);
51 private:
52 DispatcherApi();
53 Event* event_;
55 DISALLOW_COPY_AND_ASSIGN(DispatcherApi);
58 const base::NativeEvent& native_event() const { return native_event_; }
59 EventType type() const { return type_; }
60 const std::string& name() const { return name_; }
61 // time_stamp represents time since machine was booted.
62 const base::TimeDelta& time_stamp() const { return time_stamp_; }
63 int flags() const { return flags_; }
65 // This is only intended to be used externally by classes that are modifying
66 // events in an EventRewriter.
67 void set_flags(int flags) { flags_ = flags; }
69 EventTarget* target() const { return target_; }
70 EventPhase phase() const { return phase_; }
71 EventResult result() const { return result_; }
73 LatencyInfo* latency() { return &latency_; }
74 const LatencyInfo* latency() const { return &latency_; }
75 void set_latency(const LatencyInfo& latency) { latency_ = latency; }
77 int source_device_id() const { return source_device_id_; }
78 void set_source_device_id(int id) { source_device_id_ = id; }
80 // By default, events are "cancelable", this means any default processing that
81 // the containing abstraction layer may perform can be prevented by calling
82 // SetHandled(). SetHandled() or StopPropagation() must not be called for
83 // events that are not cancelable.
84 bool cancelable() const { return cancelable_; }
86 // The following methods return true if the respective keys were pressed at
87 // the time the event was created.
88 bool IsShiftDown() const { return (flags_ & EF_SHIFT_DOWN) != 0; }
89 bool IsControlDown() const { return (flags_ & EF_CONTROL_DOWN) != 0; }
90 bool IsCapsLockDown() const { return (flags_ & EF_CAPS_LOCK_DOWN) != 0; }
91 bool IsAltDown() const { return (flags_ & EF_ALT_DOWN) != 0; }
92 bool IsAltGrDown() const { return (flags_ & EF_ALTGR_DOWN) != 0; }
93 bool IsCommandDown() const { return (flags_ & EF_COMMAND_DOWN) != 0; }
94 bool IsRepeat() const { return (flags_ & EF_IS_REPEAT) != 0; }
96 bool IsKeyEvent() const {
97 return type_ == ET_KEY_PRESSED || type_ == ET_KEY_RELEASED;
100 bool IsMouseEvent() const {
101 return type_ == ET_MOUSE_PRESSED ||
102 type_ == ET_MOUSE_DRAGGED ||
103 type_ == ET_MOUSE_RELEASED ||
104 type_ == ET_MOUSE_MOVED ||
105 type_ == ET_MOUSE_ENTERED ||
106 type_ == ET_MOUSE_EXITED ||
107 type_ == ET_MOUSEWHEEL ||
108 type_ == ET_MOUSE_CAPTURE_CHANGED;
111 bool IsTouchEvent() const {
112 return type_ == ET_TOUCH_RELEASED ||
113 type_ == ET_TOUCH_PRESSED ||
114 type_ == ET_TOUCH_MOVED ||
115 type_ == ET_TOUCH_CANCELLED;
118 bool IsGestureEvent() const {
119 switch (type_) {
120 case ET_GESTURE_SCROLL_BEGIN:
121 case ET_GESTURE_SCROLL_END:
122 case ET_GESTURE_SCROLL_UPDATE:
123 case ET_GESTURE_TAP:
124 case ET_GESTURE_TAP_CANCEL:
125 case ET_GESTURE_TAP_DOWN:
126 case ET_GESTURE_BEGIN:
127 case ET_GESTURE_END:
128 case ET_GESTURE_TWO_FINGER_TAP:
129 case ET_GESTURE_PINCH_BEGIN:
130 case ET_GESTURE_PINCH_END:
131 case ET_GESTURE_PINCH_UPDATE:
132 case ET_GESTURE_LONG_PRESS:
133 case ET_GESTURE_LONG_TAP:
134 case ET_GESTURE_SWIPE:
135 case ET_GESTURE_SHOW_PRESS:
136 case ET_GESTURE_WIN8_EDGE_SWIPE:
137 // When adding a gesture event which is paired with an event which
138 // occurs earlier, add the event to |IsEndingEvent|.
139 return true;
141 case ET_SCROLL_FLING_CANCEL:
142 case ET_SCROLL_FLING_START:
143 // These can be ScrollEvents too. EF_FROM_TOUCH determines if they're
144 // Gesture or Scroll events.
145 return (flags_ & EF_FROM_TOUCH) == EF_FROM_TOUCH;
147 default:
148 break;
150 return false;
153 // An ending event is paired with the event which started it. Setting capture
154 // should not prevent ending events from getting to their initial target.
155 bool IsEndingEvent() const {
156 switch(type_) {
157 case ui::ET_TOUCH_CANCELLED:
158 case ui::ET_GESTURE_TAP_CANCEL:
159 case ui::ET_GESTURE_END:
160 case ui::ET_GESTURE_SCROLL_END:
161 case ui::ET_GESTURE_PINCH_END:
162 return true;
163 default:
164 return false;
168 bool IsScrollEvent() const {
169 // Flings can be GestureEvents too. EF_FROM_TOUCH determins if they're
170 // Gesture or Scroll events.
171 return type_ == ET_SCROLL ||
172 ((type_ == ET_SCROLL_FLING_START ||
173 type_ == ET_SCROLL_FLING_CANCEL) &&
174 !(flags() & EF_FROM_TOUCH));
177 bool IsScrollGestureEvent() const {
178 return type_ == ET_GESTURE_SCROLL_BEGIN ||
179 type_ == ET_GESTURE_SCROLL_UPDATE ||
180 type_ == ET_GESTURE_SCROLL_END;
183 bool IsFlingScrollEvent() const {
184 return type_ == ET_SCROLL_FLING_CANCEL ||
185 type_ == ET_SCROLL_FLING_START;
188 bool IsMouseWheelEvent() const {
189 return type_ == ET_MOUSEWHEEL;
192 bool IsLocatedEvent() const {
193 return IsMouseEvent() || IsScrollEvent() || IsTouchEvent() ||
194 IsGestureEvent();
197 // Convenience methods to cast |this| to a GestureEvent. IsGestureEvent()
198 // must be true as a precondition to calling these methods.
199 GestureEvent* AsGestureEvent();
200 const GestureEvent* AsGestureEvent() const;
202 // Returns true if the event has a valid |native_event_|.
203 bool HasNativeEvent() const;
205 // Immediately stops the propagation of the event. This must be called only
206 // from an EventHandler during an event-dispatch. Any event handler that may
207 // be in the list will not receive the event after this is called.
208 // Note that StopPropagation() can be called only for cancelable events.
209 void StopPropagation();
210 bool stopped_propagation() const { return !!(result_ & ER_CONSUMED); }
212 // Marks the event as having been handled. A handled event does not reach the
213 // next event phase. For example, if an event is handled during the pre-target
214 // phase, then the event is dispatched to all pre-target handlers, but not to
215 // the target or post-target handlers.
216 // Note that SetHandled() can be called only for cancelable events.
217 void SetHandled();
218 bool handled() const { return result_ != ER_UNHANDLED; }
220 protected:
221 Event(EventType type, base::TimeDelta time_stamp, int flags);
222 Event(const base::NativeEvent& native_event, EventType type, int flags);
223 Event(const Event& copy);
224 void SetType(EventType type);
225 void set_cancelable(bool cancelable) { cancelable_ = cancelable; }
227 void set_time_stamp(const base::TimeDelta& time_stamp) {
228 time_stamp_ = time_stamp;
231 void set_name(const std::string& name) { name_ = name; }
233 private:
234 friend class EventTestApi;
236 EventType type_;
237 std::string name_;
238 base::TimeDelta time_stamp_;
239 LatencyInfo latency_;
240 int flags_;
241 base::NativeEvent native_event_;
242 bool delete_native_event_;
243 bool cancelable_;
244 EventTarget* target_;
245 EventPhase phase_;
246 EventResult result_;
248 // The device id the event came from, or ED_UNKNOWN_DEVICE if the information
249 // is not available.
250 int source_device_id_;
253 class EVENTS_EXPORT CancelModeEvent : public Event {
254 public:
255 CancelModeEvent();
256 ~CancelModeEvent() override;
259 class EVENTS_EXPORT LocatedEvent : public Event {
260 public:
261 ~LocatedEvent() override;
263 float x() const { return location_.x(); }
264 float y() const { return location_.y(); }
265 void set_location(const gfx::PointF& location) { location_ = location; }
266 gfx::Point location() const { return gfx::ToFlooredPoint(location_); }
267 const gfx::PointF& location_f() const { return location_; }
268 void set_root_location(const gfx::PointF& root_location) {
269 root_location_ = root_location;
271 gfx::Point root_location() const {
272 return gfx::ToFlooredPoint(root_location_);
274 const gfx::PointF& root_location_f() const {
275 return root_location_;
278 // Transform the locations using |inverted_root_transform|.
279 // This is applied to both |location_| and |root_location_|.
280 virtual void UpdateForRootTransform(
281 const gfx::Transform& inverted_root_transform);
283 template <class T> void ConvertLocationToTarget(T* source, T* target) {
284 if (!target || target == source)
285 return;
286 gfx::Point offset = gfx::ToFlooredPoint(location_);
287 T::ConvertPointToTarget(source, target, &offset);
288 gfx::Vector2d diff = gfx::ToFlooredPoint(location_) - offset;
289 location_= location_ - diff;
292 protected:
293 friend class LocatedEventTestApi;
294 explicit LocatedEvent(const base::NativeEvent& native_event);
296 // Create a new LocatedEvent which is identical to the provided model.
297 // If source / target windows are provided, the model location will be
298 // converted from |source| coordinate system to |target| coordinate system.
299 template <class T>
300 LocatedEvent(const LocatedEvent& model, T* source, T* target)
301 : Event(model),
302 location_(model.location_),
303 root_location_(model.root_location_) {
304 ConvertLocationToTarget(source, target);
307 // Used for synthetic events in testing.
308 LocatedEvent(EventType type,
309 const gfx::PointF& location,
310 const gfx::PointF& root_location,
311 base::TimeDelta time_stamp,
312 int flags);
314 gfx::PointF location_;
316 // |location_| multiplied by an optional transformation matrix for
317 // rotations, animations and skews.
318 gfx::PointF root_location_;
321 // Structure for handling common fields between touch and mouse to support
322 // PointerEvents API.
323 class EVENTS_EXPORT PointerDetails {
324 public:
325 PointerDetails() {}
326 explicit PointerDetails(EventPointerType pointer_type)
327 : pointer_type_(pointer_type) {}
328 PointerDetails(EventPointerType pointer_type,
329 float radius_x,
330 float radius_y,
331 float force,
332 float tilt_x,
333 float tilt_y)
334 : pointer_type_(pointer_type),
335 radius_x_(radius_x),
336 radius_y_(radius_y),
337 force_(force),
338 tilt_x_(tilt_x),
339 tilt_y_(tilt_y) {}
341 EventPointerType pointer_type() const { return pointer_type_; };
343 // If we aren't provided with a radius on one axis, use the
344 // information from the other axis.
345 float radius_x() const { return radius_x_ > 0 ? radius_x_ : radius_y_; }
346 float radius_y() const { return radius_y_ > 0 ? radius_y_ : radius_x_; }
347 float force() const { return force_; }
348 float tilt_x() const { return tilt_x_; }
349 float tilt_y() const { return tilt_y_; }
351 private:
352 // For the mutators of the members on this class.
353 friend class TouchEvent;
354 friend class MouseEvent;
356 // The type of pointer device.
357 EventPointerType pointer_type_ = EventPointerType::POINTER_TYPE_UNKNOWN;
359 // Radius of the X (major) axis of the touch ellipse. 0.0 if unknown.
360 float radius_x_ = 0.0;
362 // Radius of the Y (minor) axis of the touch ellipse. 0.0 if unknown.
363 float radius_y_ = 0.0;
365 // Force (pressure) of the touch. Normalized to be [0, 1]. Default to be 0.0.
366 float force_ = 0.0;
368 // Angle of tilt of the X (major) axis. 0.0 if unknown.
369 float tilt_x_ = 0.0;
371 // Angle of tilt of the Y (minor) axis. 0.0 if unknown.
372 float tilt_y_ = 0.0;
375 class EVENTS_EXPORT MouseEvent : public LocatedEvent {
376 public:
377 explicit MouseEvent(const base::NativeEvent& native_event);
379 // Create a new MouseEvent based on the provided model.
380 // Uses the provided |type| and |flags| for the new event.
381 // If source / target windows are provided, the model location will be
382 // converted from |source| coordinate system to |target| coordinate system.
383 template <class T>
384 MouseEvent(const MouseEvent& model, T* source, T* target)
385 : LocatedEvent(model, source, target),
386 changed_button_flags_(model.changed_button_flags_),
387 pointer_details_(model.pointer_details_) {}
389 template <class T>
390 MouseEvent(const MouseEvent& model,
391 T* source,
392 T* target,
393 EventType type,
394 int flags)
395 : LocatedEvent(model, source, target),
396 changed_button_flags_(model.changed_button_flags_),
397 pointer_details_(model.pointer_details_) {
398 SetType(type);
399 set_flags(flags);
402 // Used for synthetic events in testing, gesture recognizer and Ozone
403 MouseEvent(EventType type,
404 const gfx::PointF& location,
405 const gfx::PointF& root_location,
406 base::TimeDelta time_stamp,
407 int flags,
408 int changed_button_flags);
410 // Conveniences to quickly test what button is down
411 bool IsOnlyLeftMouseButton() const {
412 return button_flags() == EF_LEFT_MOUSE_BUTTON;
415 bool IsLeftMouseButton() const {
416 return (flags() & EF_LEFT_MOUSE_BUTTON) != 0;
419 bool IsOnlyMiddleMouseButton() const {
420 return button_flags() == EF_MIDDLE_MOUSE_BUTTON;
423 bool IsMiddleMouseButton() const {
424 return (flags() & EF_MIDDLE_MOUSE_BUTTON) != 0;
427 bool IsOnlyRightMouseButton() const {
428 return button_flags() == EF_RIGHT_MOUSE_BUTTON;
431 bool IsRightMouseButton() const {
432 return (flags() & EF_RIGHT_MOUSE_BUTTON) != 0;
435 bool IsAnyButton() const {
436 return button_flags() != 0;
439 // Compares two mouse down events and returns true if the second one should
440 // be considered a repeat of the first.
441 static bool IsRepeatedClickEvent(
442 const MouseEvent& event1,
443 const MouseEvent& event2);
445 // Get the click count. Can be 1, 2 or 3 for mousedown messages, 0 otherwise.
446 int GetClickCount() const;
448 // Set the click count for a mousedown message. Can be 1, 2 or 3.
449 void SetClickCount(int click_count);
451 // Identifies the button that changed. During a press this corresponds to the
452 // button that was pressed and during a release this corresponds to the button
453 // that was released.
454 // NOTE: during a press and release flags() contains the complete set of
455 // flags. Use this to determine the button that was pressed or released.
456 int changed_button_flags() const { return changed_button_flags_; }
458 // Updates the button that changed.
459 void set_changed_button_flags(int flags) { changed_button_flags_ = flags; }
461 // Event details common to MouseEvent and TouchEvent.
462 const PointerDetails& pointer_details() const { return pointer_details_; }
463 void set_pointer_details(const PointerDetails& details) {
464 pointer_details_ = details;
467 private:
468 FRIEND_TEST_ALL_PREFIXES(EventTest, DoubleClickRequiresRelease);
469 FRIEND_TEST_ALL_PREFIXES(EventTest, SingleClickRightLeft);
471 // Returns the flags for the mouse buttons.
472 int button_flags() const {
473 return flags() & (EF_LEFT_MOUSE_BUTTON | EF_MIDDLE_MOUSE_BUTTON |
474 EF_RIGHT_MOUSE_BUTTON | EF_BACK_MOUSE_BUTTON |
475 EF_FORWARD_MOUSE_BUTTON);
478 // Returns the repeat count based on the previous mouse click, if it is
479 // recent enough and within a small enough distance.
480 static int GetRepeatCount(const MouseEvent& click_event);
482 // Resets the last_click_event_ for unit tests.
483 static void ResetLastClickForTest();
485 // See description above getter for details.
486 int changed_button_flags_;
488 static MouseEvent* last_click_event_;
490 // We can create a MouseEvent for a native event more than once. We set this
491 // to true when the next event either has a different timestamp or we see a
492 // release signalling that the press (click) event was completed.
493 static bool last_click_complete_;
495 // Structure for holding pointer details for implementing PointerEvents API.
496 PointerDetails pointer_details_;
499 class ScrollEvent;
501 class EVENTS_EXPORT MouseWheelEvent : public MouseEvent {
502 public:
503 // See |offset| for details.
504 static const int kWheelDelta;
506 explicit MouseWheelEvent(const base::NativeEvent& native_event);
507 explicit MouseWheelEvent(const ScrollEvent& scroll_event);
508 MouseWheelEvent(const MouseEvent& mouse_event, int x_offset, int y_offset);
509 MouseWheelEvent(const MouseWheelEvent& mouse_wheel_event);
511 template <class T>
512 MouseWheelEvent(const MouseWheelEvent& model,
513 T* source,
514 T* target)
515 : MouseEvent(model, source, target, model.type(), model.flags()),
516 offset_(model.x_offset(), model.y_offset()) {
519 // Used for synthetic events in testing and by the gesture recognizer.
520 MouseWheelEvent(const gfx::Vector2d& offset,
521 const gfx::PointF& location,
522 const gfx::PointF& root_location,
523 base::TimeDelta time_stamp,
524 int flags,
525 int changed_button_flags);
527 // The amount to scroll. This is in multiples of kWheelDelta.
528 // Note: x_offset() > 0/y_offset() > 0 means scroll left/up.
529 int x_offset() const { return offset_.x(); }
530 int y_offset() const { return offset_.y(); }
531 const gfx::Vector2d& offset() const { return offset_; }
533 private:
534 gfx::Vector2d offset_;
537 class EVENTS_EXPORT TouchEvent : public LocatedEvent {
538 public:
539 explicit TouchEvent(const base::NativeEvent& native_event);
541 // Create a new TouchEvent which is identical to the provided model.
542 // If source / target windows are provided, the model location will be
543 // converted from |source| coordinate system to |target| coordinate system.
544 template <class T>
545 TouchEvent(const TouchEvent& model, T* source, T* target)
546 : LocatedEvent(model, source, target),
547 touch_id_(model.touch_id_),
548 unique_event_id_(model.unique_event_id_),
549 rotation_angle_(model.rotation_angle_),
550 may_cause_scrolling_(model.may_cause_scrolling_),
551 should_remove_native_touch_id_mapping_(false),
552 pointer_details_(model.pointer_details_) {}
554 TouchEvent(EventType type,
555 const gfx::PointF& location,
556 int touch_id,
557 base::TimeDelta time_stamp);
559 TouchEvent(EventType type,
560 const gfx::PointF& location,
561 int flags,
562 int touch_id,
563 base::TimeDelta timestamp,
564 float radius_x,
565 float radius_y,
566 float angle,
567 float force);
569 TouchEvent(const TouchEvent& copy);
571 ~TouchEvent() override;
573 // The id of the pointer this event modifies.
574 int touch_id() const { return touch_id_; }
575 // A unique identifier for this event.
576 uint32 unique_event_id() const { return unique_event_id_; }
578 float rotation_angle() const { return rotation_angle_; }
580 void set_may_cause_scrolling(bool causes) { may_cause_scrolling_ = causes; }
581 bool may_cause_scrolling() const { return may_cause_scrolling_; }
583 // TODO(robert.bradford): ozone_platform_egltest.cc could use
584 // UpdateForRootTransform() instead: crbug.com/519337
585 void set_radius_x(const float r) { pointer_details_.radius_x_ = r; }
586 void set_radius_y(const float r) { pointer_details_.radius_y_ = r; }
588 void set_should_remove_native_touch_id_mapping(
589 bool should_remove_native_touch_id_mapping) {
590 should_remove_native_touch_id_mapping_ =
591 should_remove_native_touch_id_mapping;
594 // Overridden from LocatedEvent.
595 void UpdateForRootTransform(
596 const gfx::Transform& inverted_root_transform) override;
598 // Marks the event as not participating in synchronous gesture recognition.
599 void DisableSynchronousHandling();
600 bool synchronous_handling_disabled() const {
601 return !!(result() & ER_DISABLE_SYNC_HANDLING);
604 // Event details common to MouseEvent and TouchEvent.
605 const PointerDetails& pointer_details() const { return pointer_details_; }
607 private:
608 // Adjusts rotation_angle_ to within the acceptable range.
609 void FixRotationAngle();
611 // The identity (typically finger) of the touch starting at 0 and incrementing
612 // for each separable additional touch that the hardware can detect.
613 const int touch_id_;
615 // A unique identifier for the touch event.
616 const uint32 unique_event_id_;
618 // Clockwise angle (in degrees) of the major axis from the X axis. Must be
619 // less than 180 and non-negative.
620 float rotation_angle_;
622 // Whether the (unhandled) touch event will produce a scroll event (e.g., a
623 // touchmove that exceeds the platform slop region, or a touchend that
624 // causes a fling). Defaults to false.
625 bool may_cause_scrolling_;
627 // True if this event should remove the mapping between the native
628 // event id and the touch_id_. This should only be the case for
629 // release and cancel events where the associated touch press event
630 // created a mapping between the native id and the touch_id_.
631 bool should_remove_native_touch_id_mapping_;
633 // Structure for holding pointer details for implementing PointerEvents API.
634 PointerDetails pointer_details_;
637 // An interface that individual platforms can use to store additional data on
638 // KeyEvent.
640 // Currently only used in mojo.
641 class EVENTS_EXPORT ExtendedKeyEventData {
642 public:
643 virtual ~ExtendedKeyEventData() {}
645 virtual ExtendedKeyEventData* Clone() const = 0;
648 // A KeyEvent is really two distinct classes, melded together due to the
649 // DOM legacy of Windows key events: a keystroke event (is_char_ == false),
650 // or a character event (is_char_ == true).
652 // For a keystroke event,
653 // -- |bool is_char_| is false.
654 // -- |EventType Event::type()| can be ET_KEY_PRESSED or ET_KEY_RELEASED.
655 // -- |DomCode code_| and |int Event::flags()| represent the physical key event.
656 // - code_ is a platform-independent representation of the physical key,
657 // based on DOM UI Events KeyboardEvent |code| values. It does not
658 // vary depending on key layout.
659 // http://www.w3.org/TR/DOM-Level-3-Events-code/
660 // - Event::flags() provides the active modifiers for the physical key
661 // press. Its value reflects the state after the event; that is, for
662 // a modifier key, a press includes the corresponding flag and a release
663 // does not.
664 // -- |DomKey key_| provides the meaning (character or action) of the key
665 // event, in the context of the active layout and modifiers. It corresponds
666 // to DOM UI Events KeyboardEvent |key| values.
667 // http://www.w3.org/TR/DOM-Level-3-Events-key/
668 // -- |KeyboardCode key_code_| supports the legacy web event |keyCode| field,
669 // and its VKEY_ values are chosen to match Windows/IE for compatibility.
670 // For printable characters, this may or may not be a layout-mapped value,
671 // imitating MS Windows: if the mapped key generates a character that has
672 // an associated VKEY_ code, then key_code_ is that code; if not, then
673 // key_code_ is the unmapped VKEY_ code. For example, US, Greek, Cyrillic,
674 // Japanese, etc. all use VKEY_Q for the key beside Tab, while French uses
675 // VKEY_A. The stored key_code_ is non-located (e.g. VKEY_SHIFT rather than
676 // VKEY_LSHIFT, VKEY_1 rather than VKEY_NUMPAD1).
678 // For a character event,
679 // -- |bool is_char_| is true.
680 // -- |EventType Event::type()| is ET_KEY_PRESSED.
681 // -- |DomCode code_| is DomCode::NONE.
682 // -- |DomKey key_| is a UTF-16 code point.
683 // -- |KeyboardCode key_code_| is conflated with the character-valued key_
684 // by some code, because both arrive in the wParam field of a Windows event.
686 class EVENTS_EXPORT KeyEvent : public Event {
687 public:
688 // Create a KeyEvent from a NativeEvent. For Windows this native event can
689 // be either a keystroke message (WM_KEYUP/WM_KEYDOWN) or a character message
690 // (WM_CHAR). Other systems have only keystroke events.
691 explicit KeyEvent(const base::NativeEvent& native_event);
693 // Create a keystroke event from a legacy KeyboardCode.
694 // This should not be used in new code.
695 KeyEvent(EventType type, KeyboardCode key_code, int flags);
697 // Create a fully defined keystroke event.
698 KeyEvent(EventType type,
699 KeyboardCode key_code,
700 DomCode code,
701 int flags,
702 DomKey key,
703 base::TimeDelta time_stamp);
705 // Create a character event.
706 KeyEvent(base::char16 character, KeyboardCode key_code, int flags);
708 // Used for synthetic events with code of DOM KeyboardEvent (e.g. 'KeyA')
709 // See also: ui/events/keycodes/dom/dom_values.txt
710 KeyEvent(EventType type,
711 KeyboardCode key_code,
712 DomCode code,
713 int flags);
715 KeyEvent(const KeyEvent& rhs);
717 KeyEvent& operator=(const KeyEvent& rhs);
719 ~KeyEvent() override;
721 // TODO(erg): While we transition to mojo, we have to hack around a mismatch
722 // in our event types. Our ui::Events don't really have all the data we need
723 // to process key events, and we instead do per-platform conversions with
724 // native HWNDs or XEvents. And we can't reliably send those native data
725 // types across mojo types in a cross-platform way. So instead, we set the
726 // resulting data when read across IPC boundaries.
727 void SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data);
728 const ExtendedKeyEventData* extended_key_event_data() const {
729 return extended_key_event_data_.get();
732 // This bypasses the normal mapping from keystroke events to characters,
733 // which allows an I18N virtual keyboard to fabricate a keyboard event that
734 // does not have a corresponding KeyboardCode (example: U+00E1 Latin small
735 // letter A with acute, U+0410 Cyrillic capital letter A).
736 void set_character(base::char16 character) {
737 key_ = DomKey::FromCharacter(character);
740 // Gets the character generated by this key event. It only supports Unicode
741 // BMP characters.
742 base::char16 GetCharacter() const;
744 // If this is a keystroke event with key_code_ VKEY_RETURN, returns '\r';
745 // otherwise returns the same as GetCharacter().
746 base::char16 GetUnmodifiedText() const;
748 // If the Control key is down in the event, returns a layout-independent
749 // character (corresponding to US layout); otherwise returns the same
750 // as GetUnmodifiedText().
751 base::char16 GetText() const;
753 // Gets the associated (Windows-based) KeyboardCode for this key event.
754 // Historically, this has also been used to obtain the character associated
755 // with a character event, because both use the Window message 'wParam' field.
756 // This should be avoided; if necessary for backwards compatibility, use
757 // GetConflatedWindowsKeyCode().
758 KeyboardCode key_code() const { return key_code_; }
760 // True if this is a character event, false if this is a keystroke event.
761 bool is_char() const { return is_char_; }
763 // This is only intended to be used externally by classes that are modifying
764 // events in an EventRewriter.
765 void set_key_code(KeyboardCode key_code) { key_code_ = key_code; }
767 // Returns the same value as key_code(), except that located codes are
768 // returned in place of non-located ones (e.g. VKEY_LSHIFT or VKEY_RSHIFT
769 // instead of VKEY_SHIFT). This is a hybrid of semantic and physical
770 // for legacy DOM reasons.
771 KeyboardCode GetLocatedWindowsKeyboardCode() const;
773 // For a keystroke event, returns the same value as key_code().
774 // For a character event, returns the same value as GetCharacter().
775 // This exists for backwards compatibility with Windows key events.
776 uint16 GetConflatedWindowsKeyCode() const;
778 // Returns true for [Alt]+<num-pad digit> Unicode alt key codes used by Win.
779 // TODO(msw): Additional work may be needed for analogues on other platforms.
780 bool IsUnicodeKeyCode() const;
782 // Returns the DOM .code (physical key identifier) for a keystroke event.
783 DomCode code() const { return code_; };
784 std::string GetCodeString() const;
786 // Returns the DOM .key (layout meaning) for a keystroke event.
787 DomKey GetDomKey() const;
789 // Normalizes flags_ so that it describes the state after the event.
790 // (Native X11 event flags describe the state before the event.)
791 void NormalizeFlags();
793 protected:
794 friend class KeyEventTestApi;
796 // This allows a subclass TranslatedKeyEvent to be a non character event.
797 void set_is_char(bool is_char) { is_char_ = is_char; }
799 private:
800 // Determine key_ on a keystroke event from code_ and flags().
801 void ApplyLayout() const;
803 KeyboardCode key_code_;
805 // DOM KeyboardEvent |code| (e.g. DomCode::KEY_A, DomCode::SPACE).
806 // http://www.w3.org/TR/DOM-Level-3-Events-code/
808 // This value represents the physical position in the keyboard and can be
809 // converted from / to keyboard scan code like XKB.
810 DomCode code_;
812 // True if this is a character event, false if this is a keystroke event.
813 bool is_char_ = false;
815 // TODO(kpschoedel): refactor so that key_ is not mutable.
816 // This requires defining the KeyEvent completely at construction rather
817 // than lazily under GetCharacter(), which likely also means removing
818 // the two 'incomplete' constructors. crbug.com/444045
820 // DOM KeyboardEvent |key|
821 // http://www.w3.org/TR/DOM-Level-3-Events-key/
823 // This value represents the meaning of a key, which is either a Unicode
824 // character, or a named DomKey:: value.
825 // This is not necessarily initialized when the event is constructed;
826 // it may be set only if and when GetCharacter() or GetDomKey() is called.
827 mutable DomKey key_ = DomKey::NONE;
829 // Parts of our event handling require raw native events (see both the
830 // windows and linux implementations of web_input_event in content/). Because
831 // mojo instead serializes and deserializes events in potentially different
832 // processes, we need to have a mechanism to keep track of this data.
833 scoped_ptr<ExtendedKeyEventData> extended_key_event_data_;
835 static bool IsRepeated(const KeyEvent& event);
837 static KeyEvent* last_key_event_;
840 class EVENTS_EXPORT ScrollEvent : public MouseEvent {
841 public:
842 explicit ScrollEvent(const base::NativeEvent& native_event);
843 template <class T>
844 ScrollEvent(const ScrollEvent& model,
845 T* source,
846 T* target)
847 : MouseEvent(model, source, target),
848 x_offset_(model.x_offset_),
849 y_offset_(model.y_offset_),
850 x_offset_ordinal_(model.x_offset_ordinal_),
851 y_offset_ordinal_(model.y_offset_ordinal_),
852 finger_count_(model.finger_count_){
855 // Used for tests.
856 ScrollEvent(EventType type,
857 const gfx::PointF& location,
858 base::TimeDelta time_stamp,
859 int flags,
860 float x_offset,
861 float y_offset,
862 float x_offset_ordinal,
863 float y_offset_ordinal,
864 int finger_count);
866 // Scale the scroll event's offset value.
867 // This is useful in the multi-monitor setup where it needs to be scaled
868 // to provide a consistent user experience.
869 void Scale(const float factor);
871 float x_offset() const { return x_offset_; }
872 float y_offset() const { return y_offset_; }
873 float x_offset_ordinal() const { return x_offset_ordinal_; }
874 float y_offset_ordinal() const { return y_offset_ordinal_; }
875 int finger_count() const { return finger_count_; }
877 private:
878 // Potential accelerated offsets.
879 float x_offset_;
880 float y_offset_;
881 // Unaccelerated offsets.
882 float x_offset_ordinal_;
883 float y_offset_ordinal_;
884 // Number of fingers on the pad.
885 int finger_count_;
888 class EVENTS_EXPORT GestureEvent : public LocatedEvent {
889 public:
890 GestureEvent(float x,
891 float y,
892 int flags,
893 base::TimeDelta time_stamp,
894 const GestureEventDetails& details);
896 // Create a new GestureEvent which is identical to the provided model.
897 // If source / target windows are provided, the model location will be
898 // converted from |source| coordinate system to |target| coordinate system.
899 template <typename T>
900 GestureEvent(const GestureEvent& model, T* source, T* target)
901 : LocatedEvent(model, source, target),
902 details_(model.details_) {
905 ~GestureEvent() override;
907 const GestureEventDetails& details() const { return details_; }
909 private:
910 GestureEventDetails details_;
913 } // namespace ui
915 #endif // UI_EVENTS_EVENT_H_