platformKeys: Add per-extension sign permissions.
[chromium-blink-merge.git] / ui / events / event.h
blob28a19104af906515b85b902490ee65b62b2502bc
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/keyboard_codes.h"
19 #include "ui/events/latency_info.h"
20 #include "ui/gfx/geometry/point.h"
21 #include "ui/gfx/geometry/point_conversions.h"
23 namespace gfx {
24 class Transform;
27 namespace ui {
28 class EventTarget;
29 enum class DomCode;
30 enum class DomKey;
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 ||
98 type_ == ET_KEY_RELEASED ||
99 type_ == ET_TRANSLATED_KEY_PRESS ||
100 type_ == ET_TRANSLATED_KEY_RELEASE;
103 bool IsMouseEvent() const {
104 return type_ == ET_MOUSE_PRESSED ||
105 type_ == ET_MOUSE_DRAGGED ||
106 type_ == ET_MOUSE_RELEASED ||
107 type_ == ET_MOUSE_MOVED ||
108 type_ == ET_MOUSE_ENTERED ||
109 type_ == ET_MOUSE_EXITED ||
110 type_ == ET_MOUSEWHEEL ||
111 type_ == ET_MOUSE_CAPTURE_CHANGED;
114 bool IsTouchEvent() const {
115 return type_ == ET_TOUCH_RELEASED ||
116 type_ == ET_TOUCH_PRESSED ||
117 type_ == ET_TOUCH_MOVED ||
118 type_ == ET_TOUCH_CANCELLED;
121 bool IsGestureEvent() const {
122 switch (type_) {
123 case ET_GESTURE_SCROLL_BEGIN:
124 case ET_GESTURE_SCROLL_END:
125 case ET_GESTURE_SCROLL_UPDATE:
126 case ET_GESTURE_TAP:
127 case ET_GESTURE_TAP_CANCEL:
128 case ET_GESTURE_TAP_DOWN:
129 case ET_GESTURE_BEGIN:
130 case ET_GESTURE_END:
131 case ET_GESTURE_TWO_FINGER_TAP:
132 case ET_GESTURE_PINCH_BEGIN:
133 case ET_GESTURE_PINCH_END:
134 case ET_GESTURE_PINCH_UPDATE:
135 case ET_GESTURE_LONG_PRESS:
136 case ET_GESTURE_LONG_TAP:
137 case ET_GESTURE_SWIPE:
138 case ET_GESTURE_SHOW_PRESS:
139 case ET_GESTURE_WIN8_EDGE_SWIPE:
140 // When adding a gesture event which is paired with an event which
141 // occurs earlier, add the event to |IsEndingEvent|.
142 return true;
144 case ET_SCROLL_FLING_CANCEL:
145 case ET_SCROLL_FLING_START:
146 // These can be ScrollEvents too. EF_FROM_TOUCH determines if they're
147 // Gesture or Scroll events.
148 return (flags_ & EF_FROM_TOUCH) == EF_FROM_TOUCH;
150 default:
151 break;
153 return false;
156 // An ending event is paired with the event which started it. Setting capture
157 // should not prevent ending events from getting to their initial target.
158 bool IsEndingEvent() const {
159 switch(type_) {
160 case ui::ET_TOUCH_CANCELLED:
161 case ui::ET_GESTURE_TAP_CANCEL:
162 case ui::ET_GESTURE_END:
163 case ui::ET_GESTURE_SCROLL_END:
164 case ui::ET_GESTURE_PINCH_END:
165 return true;
166 default:
167 return false;
171 bool IsScrollEvent() const {
172 // Flings can be GestureEvents too. EF_FROM_TOUCH determins if they're
173 // Gesture or Scroll events.
174 return type_ == ET_SCROLL ||
175 ((type_ == ET_SCROLL_FLING_START ||
176 type_ == ET_SCROLL_FLING_CANCEL) &&
177 !(flags() & EF_FROM_TOUCH));
180 bool IsScrollGestureEvent() const {
181 return type_ == ET_GESTURE_SCROLL_BEGIN ||
182 type_ == ET_GESTURE_SCROLL_UPDATE ||
183 type_ == ET_GESTURE_SCROLL_END;
186 bool IsFlingScrollEvent() const {
187 return type_ == ET_SCROLL_FLING_CANCEL ||
188 type_ == ET_SCROLL_FLING_START;
191 bool IsMouseWheelEvent() const {
192 return type_ == ET_MOUSEWHEEL;
195 bool IsLocatedEvent() const {
196 return IsMouseEvent() || IsScrollEvent() || IsTouchEvent() ||
197 IsGestureEvent();
200 // Convenience methods to cast |this| to a GestureEvent. IsGestureEvent()
201 // must be true as a precondition to calling these methods.
202 GestureEvent* AsGestureEvent();
203 const GestureEvent* AsGestureEvent() const;
205 // Returns true if the event has a valid |native_event_|.
206 bool HasNativeEvent() const;
208 // Immediately stops the propagation of the event. This must be called only
209 // from an EventHandler during an event-dispatch. Any event handler that may
210 // be in the list will not receive the event after this is called.
211 // Note that StopPropagation() can be called only for cancelable events.
212 void StopPropagation();
213 bool stopped_propagation() const { return !!(result_ & ER_CONSUMED); }
215 // Marks the event as having been handled. A handled event does not reach the
216 // next event phase. For example, if an event is handled during the pre-target
217 // phase, then the event is dispatched to all pre-target handlers, but not to
218 // the target or post-target handlers.
219 // Note that SetHandled() can be called only for cancelable events.
220 void SetHandled();
221 bool handled() const { return result_ != ER_UNHANDLED; }
223 protected:
224 Event(EventType type, base::TimeDelta time_stamp, int flags);
225 Event(const base::NativeEvent& native_event, EventType type, int flags);
226 Event(const Event& copy);
227 void SetType(EventType type);
228 void set_cancelable(bool cancelable) { cancelable_ = cancelable; }
230 void set_time_stamp(const base::TimeDelta& time_stamp) {
231 time_stamp_ = time_stamp;
234 void set_name(const std::string& name) { name_ = name; }
236 private:
237 friend class EventTestApi;
239 EventType type_;
240 std::string name_;
241 base::TimeDelta time_stamp_;
242 LatencyInfo latency_;
243 int flags_;
244 base::NativeEvent native_event_;
245 bool delete_native_event_;
246 bool cancelable_;
247 EventTarget* target_;
248 EventPhase phase_;
249 EventResult result_;
251 // The device id the event came from, or ED_UNKNOWN_DEVICE if the information
252 // is not available.
253 int source_device_id_;
256 class EVENTS_EXPORT CancelModeEvent : public Event {
257 public:
258 CancelModeEvent();
259 ~CancelModeEvent() override;
262 class EVENTS_EXPORT LocatedEvent : public Event {
263 public:
264 ~LocatedEvent() override;
266 float x() const { return location_.x(); }
267 float y() const { return location_.y(); }
268 void set_location(const gfx::PointF& location) { location_ = location; }
269 // TODO(tdresser): Always return floating point location. See
270 // crbug.com/337824.
271 gfx::Point location() const { return gfx::ToFlooredPoint(location_); }
272 const gfx::PointF& location_f() const { return location_; }
273 void set_root_location(const gfx::PointF& root_location) {
274 root_location_ = root_location;
276 gfx::Point root_location() const {
277 return gfx::ToFlooredPoint(root_location_);
279 const gfx::PointF& root_location_f() const {
280 return root_location_;
283 // Transform the locations using |inverted_root_transform|.
284 // This is applied to both |location_| and |root_location_|.
285 virtual void UpdateForRootTransform(
286 const gfx::Transform& inverted_root_transform);
288 template <class T> void ConvertLocationToTarget(T* source, T* target) {
289 if (!target || target == source)
290 return;
291 // TODO(tdresser): Rewrite ConvertPointToTarget to use PointF. See
292 // crbug.com/337824.
293 gfx::Point offset = gfx::ToFlooredPoint(location_);
294 T::ConvertPointToTarget(source, target, &offset);
295 gfx::Vector2d diff = gfx::ToFlooredPoint(location_) - offset;
296 location_= location_ - diff;
299 protected:
300 friend class LocatedEventTestApi;
301 explicit LocatedEvent(const base::NativeEvent& native_event);
303 // Create a new LocatedEvent which is identical to the provided model.
304 // If source / target windows are provided, the model location will be
305 // converted from |source| coordinate system to |target| coordinate system.
306 template <class T>
307 LocatedEvent(const LocatedEvent& model, T* source, T* target)
308 : Event(model),
309 location_(model.location_),
310 root_location_(model.root_location_) {
311 ConvertLocationToTarget(source, target);
314 // Used for synthetic events in testing.
315 LocatedEvent(EventType type,
316 const gfx::PointF& location,
317 const gfx::PointF& root_location,
318 base::TimeDelta time_stamp,
319 int flags);
321 gfx::PointF location_;
323 // |location_| multiplied by an optional transformation matrix for
324 // rotations, animations and skews.
325 gfx::PointF root_location_;
328 class EVENTS_EXPORT MouseEvent : public LocatedEvent {
329 public:
330 explicit MouseEvent(const base::NativeEvent& native_event);
332 // Create a new MouseEvent based on the provided model.
333 // Uses the provided |type| and |flags| for the new event.
334 // If source / target windows are provided, the model location will be
335 // converted from |source| coordinate system to |target| coordinate system.
336 template <class T>
337 MouseEvent(const MouseEvent& model, T* source, T* target)
338 : LocatedEvent(model, source, target),
339 changed_button_flags_(model.changed_button_flags_) {
342 template <class T>
343 MouseEvent(const MouseEvent& model,
344 T* source,
345 T* target,
346 EventType type,
347 int flags)
348 : LocatedEvent(model, source, target),
349 changed_button_flags_(model.changed_button_flags_) {
350 SetType(type);
351 set_flags(flags);
354 // Used for synthetic events in testing, gesture recognizer and Ozone
355 MouseEvent(EventType type,
356 const gfx::PointF& location,
357 const gfx::PointF& root_location,
358 base::TimeDelta time_stamp,
359 int flags,
360 int changed_button_flags);
362 // Conveniences to quickly test what button is down
363 bool IsOnlyLeftMouseButton() const {
364 return (flags() & EF_LEFT_MOUSE_BUTTON) &&
365 !(flags() & (EF_MIDDLE_MOUSE_BUTTON | EF_RIGHT_MOUSE_BUTTON));
368 bool IsLeftMouseButton() const {
369 return (flags() & EF_LEFT_MOUSE_BUTTON) != 0;
372 bool IsOnlyMiddleMouseButton() const {
373 return (flags() & EF_MIDDLE_MOUSE_BUTTON) &&
374 !(flags() & (EF_LEFT_MOUSE_BUTTON | EF_RIGHT_MOUSE_BUTTON));
377 bool IsMiddleMouseButton() const {
378 return (flags() & EF_MIDDLE_MOUSE_BUTTON) != 0;
381 bool IsOnlyRightMouseButton() const {
382 return (flags() & EF_RIGHT_MOUSE_BUTTON) &&
383 !(flags() & (EF_LEFT_MOUSE_BUTTON | EF_MIDDLE_MOUSE_BUTTON));
386 bool IsRightMouseButton() const {
387 return (flags() & EF_RIGHT_MOUSE_BUTTON) != 0;
390 bool IsAnyButton() const {
391 return (flags() & (EF_LEFT_MOUSE_BUTTON | EF_MIDDLE_MOUSE_BUTTON |
392 EF_RIGHT_MOUSE_BUTTON)) != 0;
395 // Compares two mouse down events and returns true if the second one should
396 // be considered a repeat of the first.
397 static bool IsRepeatedClickEvent(
398 const MouseEvent& event1,
399 const MouseEvent& event2);
401 // Get the click count. Can be 1, 2 or 3 for mousedown messages, 0 otherwise.
402 int GetClickCount() const;
404 // Set the click count for a mousedown message. Can be 1, 2 or 3.
405 void SetClickCount(int click_count);
407 // Identifies the button that changed. During a press this corresponds to the
408 // button that was pressed and during a release this corresponds to the button
409 // that was released.
410 // NOTE: during a press and release flags() contains the complete set of
411 // flags. Use this to determine the button that was pressed or released.
412 int changed_button_flags() const { return changed_button_flags_; }
414 // Updates the button that changed.
415 void set_changed_button_flags(int flags) { changed_button_flags_ = flags; }
417 private:
418 FRIEND_TEST_ALL_PREFIXES(EventTest, DoubleClickRequiresRelease);
419 FRIEND_TEST_ALL_PREFIXES(EventTest, SingleClickRightLeft);
421 // Returns the repeat count based on the previous mouse click, if it is
422 // recent enough and within a small enough distance.
423 static int GetRepeatCount(const MouseEvent& click_event);
425 // Resets the last_click_event_ for unit tests.
426 static void ResetLastClickForTest();
428 // See description above getter for details.
429 int changed_button_flags_;
431 static MouseEvent* last_click_event_;
433 // We can create a MouseEvent for a native event more than once. We set this
434 // to true when the next event either has a different timestamp or we see a
435 // release signalling that the press (click) event was completed.
436 static bool last_click_complete_;
439 class ScrollEvent;
441 class EVENTS_EXPORT MouseWheelEvent : public MouseEvent {
442 public:
443 // See |offset| for details.
444 static const int kWheelDelta;
446 explicit MouseWheelEvent(const base::NativeEvent& native_event);
447 explicit MouseWheelEvent(const ScrollEvent& scroll_event);
448 MouseWheelEvent(const MouseEvent& mouse_event, int x_offset, int y_offset);
449 MouseWheelEvent(const MouseWheelEvent& mouse_wheel_event);
451 template <class T>
452 MouseWheelEvent(const MouseWheelEvent& model,
453 T* source,
454 T* target)
455 : MouseEvent(model, source, target, model.type(), model.flags()),
456 offset_(model.x_offset(), model.y_offset()) {
459 // Used for synthetic events in testing and by the gesture recognizer.
460 MouseWheelEvent(const gfx::Vector2d& offset,
461 const gfx::PointF& location,
462 const gfx::PointF& root_location,
463 int flags,
464 int changed_button_flags);
466 // The amount to scroll. This is in multiples of kWheelDelta.
467 // Note: x_offset() > 0/y_offset() > 0 means scroll left/up.
468 int x_offset() const { return offset_.x(); }
469 int y_offset() const { return offset_.y(); }
470 const gfx::Vector2d& offset() const { return offset_; }
472 // Overridden from LocatedEvent.
473 void UpdateForRootTransform(
474 const gfx::Transform& inverted_root_transform) override;
476 private:
477 gfx::Vector2d offset_;
480 class EVENTS_EXPORT TouchEvent : public LocatedEvent {
481 public:
482 explicit TouchEvent(const base::NativeEvent& native_event);
484 // Create a new TouchEvent which is identical to the provided model.
485 // If source / target windows are provided, the model location will be
486 // converted from |source| coordinate system to |target| coordinate system.
487 template <class T>
488 TouchEvent(const TouchEvent& model, T* source, T* target)
489 : LocatedEvent(model, source, target),
490 touch_id_(model.touch_id_),
491 unique_event_id_(model.unique_event_id_),
492 radius_x_(model.radius_x_),
493 radius_y_(model.radius_y_),
494 rotation_angle_(model.rotation_angle_),
495 force_(model.force_),
496 may_cause_scrolling_(model.may_cause_scrolling_),
497 should_remove_native_touch_id_mapping_(false) {}
499 TouchEvent(EventType type,
500 const gfx::PointF& location,
501 int touch_id,
502 base::TimeDelta time_stamp);
504 TouchEvent(EventType type,
505 const gfx::PointF& location,
506 int flags,
507 int touch_id,
508 base::TimeDelta timestamp,
509 float radius_x,
510 float radius_y,
511 float angle,
512 float force);
514 ~TouchEvent() override;
516 // The id of the pointer this event modifies.
517 int touch_id() const { return touch_id_; }
518 // A unique identifier for this event.
519 uint64 unique_event_id() const { return unique_event_id_; }
520 // If we aren't provided with a radius on one axis, use the
521 // information from the other axis.
522 float radius_x() const { return radius_x_ > 0 ? radius_x_ : radius_y_; }
523 float radius_y() const { return radius_y_ > 0 ? radius_y_ : radius_x_; }
524 float rotation_angle() const { return rotation_angle_; }
525 float force() const { return force_; }
527 void set_may_cause_scrolling(bool causes) { may_cause_scrolling_ = causes; }
528 bool may_cause_scrolling() const { return may_cause_scrolling_; }
530 // Used for unit tests.
531 void set_radius_x(const float r) { radius_x_ = r; }
532 void set_radius_y(const float r) { radius_y_ = r; }
534 void set_should_remove_native_touch_id_mapping(
535 bool should_remove_native_touch_id_mapping) {
536 should_remove_native_touch_id_mapping_ =
537 should_remove_native_touch_id_mapping;
540 // Overridden from LocatedEvent.
541 void UpdateForRootTransform(
542 const gfx::Transform& inverted_root_transform) override;
544 // Marks the event as not participating in synchronous gesture recognition.
545 void DisableSynchronousHandling();
546 bool synchronous_handling_disabled() const {
547 return !!(result() & ER_DISABLE_SYNC_HANDLING);
550 private:
551 // Adjusts rotation_angle_ to within the acceptable range.
552 void FixRotationAngle();
554 // The identity (typically finger) of the touch starting at 0 and incrementing
555 // for each separable additional touch that the hardware can detect.
556 const int touch_id_;
558 // A unique identifier for the touch event.
559 const uint64 unique_event_id_;
561 // Radius of the X (major) axis of the touch ellipse. 0.0 if unknown.
562 float radius_x_;
564 // Radius of the Y (minor) axis of the touch ellipse. 0.0 if unknown.
565 float radius_y_;
567 // Clockwise angle (in degrees) of the major axis from the X axis. Must be
568 // less than 180 and non-negative.
569 float rotation_angle_;
571 // Force (pressure) of the touch. Normalized to be [0, 1]. Default to be 0.0.
572 float force_;
574 // Whether the (unhandled) touch event will produce a scroll event (e.g., a
575 // touchmove that exceeds the platform slop region, or a touchend that
576 // causes a fling). Defaults to false.
577 bool may_cause_scrolling_;
579 // True if this event should remove the mapping between the native
580 // event id and the touch_id_. This should only be the case for
581 // release and cancel events where the associated touch press event
582 // created a mapping between the native id and the touch_id_.
583 bool should_remove_native_touch_id_mapping_;
586 // An interface that individual platforms can use to store additional data on
587 // KeyEvent.
589 // Currently only used in mojo.
590 class EVENTS_EXPORT ExtendedKeyEventData {
591 public:
592 virtual ~ExtendedKeyEventData() {}
594 virtual ExtendedKeyEventData* Clone() const = 0;
597 // A KeyEvent is really two distinct classes, melded together due to the
598 // DOM legacy of Windows key events: a keystroke event (is_char_ == false),
599 // or a character event (is_char_ == true).
601 // For a keystroke event,
602 // -- is_char_ is false.
603 // -- Event::type() can be any one of ET_KEY_PRESSED, ET_KEY_RELEASED,
604 // ET_TRANSLATED_KEY_PRESS, or ET_TRANSLATED_KEY_RELEASE.
605 // -- code_ and Event::flags() represent the physical key event.
606 // - code_ is a platform-independent representation of the physical key,
607 // based on DOM KeyboardEvent |code| values. It does not vary depending
608 // on key layout.
609 // - Event::flags() provides the active modifiers for the physical key
610 // press. Its value reflects the state after the event; that is, for
611 // a modifier key, a press includes the corresponding flag and a release
612 // does not.
613 // -- key_ and character_ provide the meaning of the key event, in the context
614 // of the active layout and modifiers. Together they correspond to DOM
615 // KeyboardEvent |key| values.
616 // - key_ is an enumeration of non-Unicode meanings, plus sentinels
617 // (specifically DomKey::CHARACTER for Unicode meanings).
618 // - character_ is the code point for Unicode meanings.
619 // -- key_code_ is a KeyboardCode value associated with the key. This supports
620 // the legacy web event |keyCode| field, and the VKEY_ values are chosen
621 // to match Windows/IE for compatibility. For printable characters, this
622 // may or may not be a layout-mapped value, imitating MS Windows:
623 // if the mapped key generates a character that has an associated VKEY_
624 // code, then key_code_ is that code; if not, then key_code_ is the unmapped
625 // VKEY_ code. For example, US, Greek, Cyrillic, Japanese, etc. all use
626 // VKEY_Q for the key beside Tab, while French uses VKEY_A.
628 // For a character event,
629 // -- is_char_ is true.
630 // -- type() is ET_KEY_PRESSED.
631 // -- code_ is DomCode::NONE.
632 // -- key_ is DomKey::CHARACTER and character_ is a UTF-16 code point.
633 // -- key_code_ is conflated with character_ by some code, because both
634 // arrive in the wParam field of a Windows event.
636 class EVENTS_EXPORT KeyEvent : public Event {
637 public:
638 // Create a KeyEvent from a NativeEvent. For Windows this native event can
639 // be either a keystroke message (WM_KEYUP/WM_KEYDOWN) or a character message
640 // (WM_CHAR). Other systems have only keystroke events.
641 explicit KeyEvent(const base::NativeEvent& native_event);
643 // Create a keystroke event.
644 KeyEvent(EventType type, KeyboardCode key_code, int flags);
646 // Create a fully defined keystroke event.
647 KeyEvent(EventType type,
648 KeyboardCode key_code,
649 DomCode code,
650 int flags,
651 DomKey key,
652 base::char16 character,
653 base::TimeDelta time_stamp);
655 // Create a character event.
656 KeyEvent(base::char16 character, KeyboardCode key_code, int flags);
658 // Used for synthetic events with code of DOM KeyboardEvent (e.g. 'KeyA')
659 // See also: ui/events/keycodes/dom3/dom_values.txt
660 KeyEvent(EventType type,
661 KeyboardCode key_code,
662 DomCode code,
663 int flags);
665 KeyEvent(const KeyEvent& rhs);
667 KeyEvent& operator=(const KeyEvent& rhs);
669 ~KeyEvent() override;
671 // TODO(erg): While we transition to mojo, we have to hack around a mismatch
672 // in our event types. Our ui::Events don't really have all the data we need
673 // to process key events, and we instead do per-platform conversions with
674 // native HWNDs or XEvents. And we can't reliably send those native data
675 // types across mojo types in a cross-platform way. So instead, we set the
676 // resulting data when read across IPC boundaries.
677 void SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data);
678 const ExtendedKeyEventData* extended_key_event_data() const {
679 return extended_key_event_data_.get();
682 // This bypasses the normal mapping from keystroke events to characters,
683 // which allows an I18N virtual keyboard to fabricate a keyboard event that
684 // does not have a corresponding KeyboardCode (example: U+00E1 Latin small
685 // letter A with acute, U+0410 Cyrillic capital letter A).
686 void set_character(base::char16 character) { character_ = character; }
688 // Gets the character generated by this key event. It only supports Unicode
689 // BMP characters.
690 base::char16 GetCharacter() const;
692 // If this is a keystroke event with key_code_ VKEY_RETURN, returns '\r';
693 // otherwise returns the same as GetCharacter().
694 base::char16 GetUnmodifiedText() const;
696 // If the Control key is down in the event, returns a layout-independent
697 // character (corresponding to US layout); otherwise returns the same
698 // as GetUnmodifiedText().
699 base::char16 GetText() const;
701 // Gets the platform key code. For XKB, this is the xksym value.
702 void set_platform_keycode(uint32 keycode) { platform_keycode_ = keycode; }
703 uint32 platform_keycode() const { return platform_keycode_; }
705 // Gets the associated (Windows-based) KeyboardCode for this key event.
706 // Historically, this has also been used to obtain the character associated
707 // with a character event, because both use the Window message 'wParam' field.
708 // This should be avoided; if necessary for backwards compatibility, use
709 // GetConflatedWindowsKeyCode().
710 KeyboardCode key_code() const { return key_code_; }
712 // True if this is a character event, false if this is a keystroke event.
713 bool is_char() const { return is_char_; }
715 // This is only intended to be used externally by classes that are modifying
716 // events in an EventRewriter.
717 void set_key_code(KeyboardCode key_code) { key_code_ = key_code; }
719 // Returns the same value as key_code(), except that located codes are
720 // returned in place of non-located ones (e.g. VKEY_LSHIFT or VKEY_RSHIFT
721 // instead of VKEY_SHIFT). This is a hybrid of semantic and physical
722 // for legacy DOM reasons.
723 KeyboardCode GetLocatedWindowsKeyboardCode() const;
725 // For a keystroke event, returns the same value as key_code().
726 // For a character event, returns the same value as GetCharacter().
727 // This exists for backwards compatibility with Windows key events.
728 uint16 GetConflatedWindowsKeyCode() const;
730 // Returns true for [Alt]+<num-pad digit> Unicode alt key codes used by Win.
731 // TODO(msw): Additional work may be needed for analogues on other platforms.
732 bool IsUnicodeKeyCode() const;
734 // Returns the DOM .code (physical key identifier) for a keystroke event.
735 DomCode code() const { return code_; };
736 std::string GetCodeString() const;
738 // Returns the DOM .key (layout meaning) for a keystroke event.
739 DomKey GetDomKey() const;
741 // Normalizes flags_ so that it describes the state after the event.
742 // (Native X11 event flags describe the state before the event.)
743 void NormalizeFlags();
745 // Returns true if the key event has already been processed by an input method
746 // and there is no need to pass the key event to the input method again.
747 bool IsTranslated() const;
748 // Marks this key event as translated or not translated.
749 void SetTranslated(bool translated);
751 protected:
752 friend class KeyEventTestApi;
754 // This allows a subclass TranslatedKeyEvent to be a non character event.
755 void set_is_char(bool is_char) { is_char_ = is_char; }
757 private:
758 // Determine key_ and character_ on a keystroke event from code_ and flags().
759 void ApplyLayout() const;
761 KeyboardCode key_code_;
763 // DOM KeyboardEvent |code| (e.g. DomCode::KEY_A, DomCode::SPACE).
764 // http://www.w3.org/TR/DOM-Level-3-Events-code/
766 // This value represents the physical position in the keyboard and can be
767 // converted from / to keyboard scan code like XKB.
768 DomCode code_;
770 // True if this is a character event, false if this is a keystroke event.
771 bool is_char_;
773 // The platform related keycode value. For XKB, it's keysym value.
774 // For now, this is used for CharacterComposer in ChromeOS.
775 mutable uint32 platform_keycode_;
777 // TODO(kpschoedel): refactor so that key_ and character_ are not mutable.
778 // This requires defining the KeyEvent completely at construction rather
779 // than lazily under GetCharacter(), which likely also means removing
780 // the two 'incomplete' constructors.
782 // DOM KeyboardEvent |key|
783 // http://www.w3.org/TR/DOM-Level-3-Events-key/
785 // This value, together with character_, represents the meaning of a key.
786 // The value is DomKey::CHARACTER when the interpretation is a character.
787 // This, along with character_, is not necessarily initialized when the
788 // event is constructed; it may be set only if and when GetCharacter()
789 // or GetDomKey() is called.
790 mutable DomKey key_;
792 // String of 'key' defined in DOM KeyboardEvent (e.g. 'a', 'รข')
793 // http://www.w3.org/TR/uievents/#keyboard-key-codes.
795 // This value represents the text that the key event will insert to input
796 // field. For key with modifier key, it may have specifial text.
797 // e.g. CTRL+A has '\x01'.
798 mutable base::char16 character_;
800 // Parts of our event handling require raw native events (see both the
801 // windows and linux implementations of web_input_event in content/). Because
802 // mojo instead serializes and deserializes events in potentially different
803 // processes, we need to have a mechanism to keep track of this data.
804 scoped_ptr<ExtendedKeyEventData> extended_key_event_data_;
806 static bool IsRepeated(const KeyEvent& event);
808 static KeyEvent* last_key_event_;
811 class EVENTS_EXPORT ScrollEvent : public MouseEvent {
812 public:
813 explicit ScrollEvent(const base::NativeEvent& native_event);
814 template <class T>
815 ScrollEvent(const ScrollEvent& model,
816 T* source,
817 T* target)
818 : MouseEvent(model, source, target),
819 x_offset_(model.x_offset_),
820 y_offset_(model.y_offset_),
821 x_offset_ordinal_(model.x_offset_ordinal_),
822 y_offset_ordinal_(model.y_offset_ordinal_),
823 finger_count_(model.finger_count_){
826 // Used for tests.
827 ScrollEvent(EventType type,
828 const gfx::PointF& location,
829 base::TimeDelta time_stamp,
830 int flags,
831 float x_offset,
832 float y_offset,
833 float x_offset_ordinal,
834 float y_offset_ordinal,
835 int finger_count);
837 // Scale the scroll event's offset value.
838 // This is useful in the multi-monitor setup where it needs to be scaled
839 // to provide a consistent user experience.
840 void Scale(const float factor);
842 float x_offset() const { return x_offset_; }
843 float y_offset() const { return y_offset_; }
844 float x_offset_ordinal() const { return x_offset_ordinal_; }
845 float y_offset_ordinal() const { return y_offset_ordinal_; }
846 int finger_count() const { return finger_count_; }
848 private:
849 // Potential accelerated offsets.
850 float x_offset_;
851 float y_offset_;
852 // Unaccelerated offsets.
853 float x_offset_ordinal_;
854 float y_offset_ordinal_;
855 // Number of fingers on the pad.
856 int finger_count_;
859 class EVENTS_EXPORT GestureEvent : public LocatedEvent {
860 public:
861 GestureEvent(float x,
862 float y,
863 int flags,
864 base::TimeDelta time_stamp,
865 const GestureEventDetails& details);
867 // Create a new GestureEvent which is identical to the provided model.
868 // If source / target windows are provided, the model location will be
869 // converted from |source| coordinate system to |target| coordinate system.
870 template <typename T>
871 GestureEvent(const GestureEvent& model, T* source, T* target)
872 : LocatedEvent(model, source, target),
873 details_(model.details_) {
876 ~GestureEvent() override;
878 const GestureEventDetails& details() const { return details_; }
880 private:
881 GestureEventDetails details_;
884 } // namespace ui
886 #endif // UI_EVENTS_EVENT_H_