OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_EVENTS_EVENT_H_ | 5 #ifndef UI_EVENTS_EVENT_H_ |
6 #define UI_EVENTS_EVENT_H_ | 6 #define UI_EVENTS_EVENT_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/event_types.h" | 10 #include "base/event_types.h" |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "ui/events/event_constants.h" | 15 #include "ui/events/event_constants.h" |
16 #include "ui/events/gesture_event_details.h" | 16 #include "ui/events/gesture_event_details.h" |
17 #include "ui/events/gestures/gesture_types.h" | 17 #include "ui/events/gestures/gesture_types.h" |
18 #include "ui/events/keycodes/keyboard_codes.h" | 18 #include "ui/events/keycodes/keyboard_codes.h" |
19 #include "ui/events/latency_info.h" | 19 #include "ui/events/latency_info.h" |
20 #include "ui/gfx/point.h" | 20 #include "ui/gfx/point.h" |
21 #include "ui/gfx/point_conversions.h" | 21 #include "ui/gfx/point_conversions.h" |
22 | 22 |
23 namespace gfx { | 23 namespace gfx { |
24 class Transform; | 24 class Transform; |
25 } | 25 } |
26 | 26 |
27 namespace ui { | 27 namespace ui { |
28 class EventTarget; | 28 class EventTarget; |
29 enum class DomCode; | 29 enum class DomCode; |
| 30 enum class DomKey; |
30 | 31 |
31 class EVENTS_EXPORT Event { | 32 class EVENTS_EXPORT Event { |
32 public: | 33 public: |
33 static scoped_ptr<Event> Clone(const Event& event); | 34 static scoped_ptr<Event> Clone(const Event& event); |
34 | 35 |
35 virtual ~Event(); | 36 virtual ~Event(); |
36 | 37 |
37 class DispatcherApi { | 38 class DispatcherApi { |
38 public: | 39 public: |
39 explicit DispatcherApi(Event* event) : event_(event) {} | 40 explicit DispatcherApi(Event* event) : event_(event) {} |
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
578 | 579 |
579 virtual ExtendedKeyEventData* Clone() const = 0; | 580 virtual ExtendedKeyEventData* Clone() const = 0; |
580 }; | 581 }; |
581 | 582 |
582 // A KeyEvent is really two distinct classes, melded together due to the | 583 // A KeyEvent is really two distinct classes, melded together due to the |
583 // DOM legacy of Windows key events: a keystroke event (is_char_ == false), | 584 // DOM legacy of Windows key events: a keystroke event (is_char_ == false), |
584 // or a character event (is_char_ == true). | 585 // or a character event (is_char_ == true). |
585 // | 586 // |
586 // For a keystroke event, | 587 // For a keystroke event, |
587 // -- is_char_ is false. | 588 // -- is_char_ is false. |
588 // -- type() can be any one of ET_KEY_PRESSED, ET_KEY_RELEASED, | 589 // -- Event::type() can be any one of ET_KEY_PRESSED, ET_KEY_RELEASED, |
589 // ET_TRANSLATED_KEY_PRESS, or ET_TRANSLATED_KEY_RELEASE. | 590 // ET_TRANSLATED_KEY_PRESS, or ET_TRANSLATED_KEY_RELEASE. |
590 // -- character_ functions as a bypass or cache for GetCharacter(). | 591 // -- code_ and Event::flags() represent the physical key event. |
591 // -- key_code_ is a VKEY_ value associated with the key. For printable | 592 // - code_ is a platform-independent representation of the physical key, |
592 // characters, this may or may not be a mapped value, imitating MS Windows: | 593 // based on DOM KeyboardEvent |code| values. It does not vary depending |
| 594 // on key layout. |
| 595 // - Event::flags() provides the active modifiers for the physical key |
| 596 // press. Its value reflects the state after the event; that is, for |
| 597 // a modifier key, a press includes the corresponding flag and a release |
| 598 // does not. |
| 599 // -- key_ and character_ provide the meaning of the key event, in the context |
| 600 // of the active layout and modifiers. Together they correspond to DOM |
| 601 // KeyboardEvent |key| values. |
| 602 // - key_ is an enumeration of non-Unicode meanings, plus sentinels |
| 603 // (specifically DomKey::CHARACTER for Unicode meanings). |
| 604 // - character_ is the code point for Unicode meanings. |
| 605 // -- key_code_ is a KeyboardCode value associated with the key. This supports |
| 606 // the legacy web event |keyCode| field, and the VKEY_ values are chosen |
| 607 // to match Windows/IE for compatibility. For printable characters, this |
| 608 // may or may not be a layout-mapped value, imitating MS Windows: |
593 // if the mapped key generates a character that has an associated VKEY_ | 609 // if the mapped key generates a character that has an associated VKEY_ |
594 // code, then key_code_ is that code; if not, then key_code_ is the unmapped | 610 // code, then key_code_ is that code; if not, then key_code_ is the unmapped |
595 // VKEY_ code. For example, US, Greek, Cyrillic, Japanese, etc. all use | 611 // VKEY_ code. For example, US, Greek, Cyrillic, Japanese, etc. all use |
596 // VKEY_Q for the key beside Tab, while French uses VKEY_A. | 612 // VKEY_Q for the key beside Tab, while French uses VKEY_A. |
597 // -- code_ is in one-to-one correspondence with a physical keyboard | |
598 // location, and does not vary depending on key layout. | |
599 // | 613 // |
600 // For a character event, | 614 // For a character event, |
601 // -- is_char_ is true. | 615 // -- is_char_ is true. |
602 // -- type() is ET_KEY_PRESSED. | 616 // -- type() is ET_KEY_PRESSED. |
603 // -- character_ is a UTF-16 character value. | 617 // -- code_ is DomCode::NONE. |
| 618 // -- key_ is DomKey::CHARACTER and character_ is a UTF-16 code point. |
604 // -- key_code_ is conflated with character_ by some code, because both | 619 // -- key_code_ is conflated with character_ by some code, because both |
605 // arrive in the wParam field of a Windows event. | 620 // arrive in the wParam field of a Windows event. |
606 // -- code_ is DomCode::NONE. | |
607 // | 621 // |
608 class EVENTS_EXPORT KeyEvent : public Event { | 622 class EVENTS_EXPORT KeyEvent : public Event { |
609 public: | 623 public: |
610 // Create a KeyEvent from a NativeEvent. For Windows this native event can | 624 // Create a KeyEvent from a NativeEvent. For Windows this native event can |
611 // be either a keystroke message (WM_KEYUP/WM_KEYDOWN) or a character message | 625 // be either a keystroke message (WM_KEYUP/WM_KEYDOWN) or a character message |
612 // (WM_CHAR). Other systems have only keystroke events. | 626 // (WM_CHAR). Other systems have only keystroke events. |
613 explicit KeyEvent(const base::NativeEvent& native_event); | 627 explicit KeyEvent(const base::NativeEvent& native_event); |
614 | 628 |
615 // Create a keystroke event. | 629 // Create a keystroke event. |
616 KeyEvent(EventType type, KeyboardCode key_code, int flags); | 630 KeyEvent(EventType type, KeyboardCode key_code, int flags); |
617 | 631 |
| 632 // Create a fully defined keystroke event. |
| 633 KeyEvent(EventType type, |
| 634 KeyboardCode key_code, |
| 635 DomCode code, |
| 636 int flags, |
| 637 DomKey key, |
| 638 base::char16 character); |
| 639 |
618 // Create a character event. | 640 // Create a character event. |
619 KeyEvent(base::char16 character, KeyboardCode key_code, int flags); | 641 KeyEvent(base::char16 character, KeyboardCode key_code, int flags); |
620 | 642 |
621 // Used for synthetic events with code of DOM KeyboardEvent (e.g. 'KeyA') | 643 // Used for synthetic events with code of DOM KeyboardEvent (e.g. 'KeyA') |
622 // See also: ui/events/keycodes/dom3/dom_values.txt | 644 // See also: ui/events/keycodes/dom3/dom_values.txt |
623 KeyEvent(EventType type, | 645 KeyEvent(EventType type, |
624 KeyboardCode key_code, | 646 KeyboardCode key_code, |
625 DomCode code, | 647 DomCode code, |
626 int flags); | 648 int flags); |
627 | 649 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 uint16 GetConflatedWindowsKeyCode() const; | 713 uint16 GetConflatedWindowsKeyCode() const; |
692 | 714 |
693 // Returns true for [Alt]+<num-pad digit> Unicode alt key codes used by Win. | 715 // Returns true for [Alt]+<num-pad digit> Unicode alt key codes used by Win. |
694 // TODO(msw): Additional work may be needed for analogues on other platforms. | 716 // TODO(msw): Additional work may be needed for analogues on other platforms. |
695 bool IsUnicodeKeyCode() const; | 717 bool IsUnicodeKeyCode() const; |
696 | 718 |
697 // Returns the DOM .code (physical key identifier) for a keystroke event. | 719 // Returns the DOM .code (physical key identifier) for a keystroke event. |
698 DomCode code() const { return code_; }; | 720 DomCode code() const { return code_; }; |
699 std::string GetCodeString() const; | 721 std::string GetCodeString() const; |
700 | 722 |
| 723 // Returns the DOM .key (layout meaning) for a keystroke event. |
| 724 DomKey GetDomKey() const; |
| 725 |
701 // Normalizes flags_ so that it describes the state after the event. | 726 // Normalizes flags_ so that it describes the state after the event. |
702 // (Native X11 event flags describe the state before the event.) | 727 // (Native X11 event flags describe the state before the event.) |
703 void NormalizeFlags(); | 728 void NormalizeFlags(); |
704 | 729 |
705 // Returns true if the key event has already been processed by an input method | 730 // Returns true if the key event has already been processed by an input method |
706 // and there is no need to pass the key event to the input method again. | 731 // and there is no need to pass the key event to the input method again. |
707 bool IsTranslated() const; | 732 bool IsTranslated() const; |
708 // Marks this key event as translated or not translated. | 733 // Marks this key event as translated or not translated. |
709 void SetTranslated(bool translated); | 734 void SetTranslated(bool translated); |
710 | 735 |
711 protected: | 736 protected: |
712 friend class KeyEventTestApi; | 737 friend class KeyEventTestApi; |
713 | 738 |
714 // This allows a subclass TranslatedKeyEvent to be a non character event. | 739 // This allows a subclass TranslatedKeyEvent to be a non character event. |
715 void set_is_char(bool is_char) { is_char_ = is_char; } | 740 void set_is_char(bool is_char) { is_char_ = is_char; } |
716 | 741 |
717 private: | 742 private: |
718 // True if the key press originated from a 'right' key (VKEY_RSHIFT, etc.). | 743 // True if the key press originated from a 'right' key (VKEY_RSHIFT, etc.). |
719 bool IsRightSideKey() const; | 744 bool IsRightSideKey() const; |
720 | 745 |
| 746 // Determine key_ and character_ on a keystroke event from code_ and flags(). |
| 747 void ApplyLayout() const; |
| 748 |
721 KeyboardCode key_code_; | 749 KeyboardCode key_code_; |
722 | 750 |
723 // DOM KeyboardEvent |code| (e.g. DomCode::KEY_A, DomCode::SPACE). | 751 // DOM KeyboardEvent |code| (e.g. DomCode::KEY_A, DomCode::SPACE). |
724 // http://www.w3.org/TR/DOM-Level-3-Events-code/ | 752 // http://www.w3.org/TR/DOM-Level-3-Events-code/ |
725 // | 753 // |
726 // This value represents the physical position in the keyboard and can be | 754 // This value represents the physical position in the keyboard and can be |
727 // converted from / to keyboard scan code like XKB. | 755 // converted from / to keyboard scan code like XKB. |
728 DomCode code_; | 756 DomCode code_; |
729 | 757 |
730 // True if this is a character event, false if this is a keystroke event. | 758 // True if this is a character event, false if this is a keystroke event. |
731 bool is_char_; | 759 bool is_char_; |
732 | 760 |
733 // The platform related keycode value. For XKB, it's keysym value. | 761 // The platform related keycode value. For XKB, it's keysym value. |
734 // For now, this is used for CharacterComposer in ChromeOS. | 762 // For now, this is used for CharacterComposer in ChromeOS. |
735 uint32 platform_keycode_; | 763 uint32 platform_keycode_; |
736 | 764 |
| 765 // TODO(kpschoedel): refactor so that key_ and character_ are not mutable. |
| 766 // This requires defining the KeyEvent completely at construction rather |
| 767 // than lazily under GetCharacter(), which likely also means removing |
| 768 // the two 'incomplete' constructors. |
| 769 // |
| 770 // DOM KeyboardEvent |key| |
| 771 // http://www.w3.org/TR/DOM-Level-3-Events-key/ |
| 772 // |
| 773 // This value, together with character_, represents the meaning of a key. |
| 774 // The value is DomKey::CHARACTER when the interpretation is a character. |
| 775 // This, along with character_, is not necessarily initialized when the |
| 776 // event is constructed; it may be set only if and when GetCharacter() |
| 777 // or GetDomKey() is called. |
| 778 mutable DomKey key_; |
| 779 |
737 // String of 'key' defined in DOM KeyboardEvent (e.g. 'a', 'â') | 780 // String of 'key' defined in DOM KeyboardEvent (e.g. 'a', 'â') |
738 // http://www.w3.org/TR/uievents/#keyboard-key-codes. | 781 // http://www.w3.org/TR/uievents/#keyboard-key-codes. |
739 // | 782 // |
740 // This value represents the text that the key event will insert to input | 783 // This value represents the text that the key event will insert to input |
741 // field. For key with modifier key, it may have specifial text. | 784 // field. For key with modifier key, it may have specifial text. |
742 // e.g. CTRL+A has '\x01'. | 785 // e.g. CTRL+A has '\x01'. |
743 mutable base::char16 character_; | 786 mutable base::char16 character_; |
744 | 787 |
745 // Parts of our event handling require raw native events (see both the | 788 // Parts of our event handling require raw native events (see both the |
746 // windows and linux implementations of web_input_event in content/). Because | 789 // windows and linux implementations of web_input_event in content/). Because |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
822 | 865 |
823 const GestureEventDetails& details() const { return details_; } | 866 const GestureEventDetails& details() const { return details_; } |
824 | 867 |
825 private: | 868 private: |
826 GestureEventDetails details_; | 869 GestureEventDetails details_; |
827 }; | 870 }; |
828 | 871 |
829 } // namespace ui | 872 } // namespace ui |
830 | 873 |
831 #endif // UI_EVENTS_EVENT_H_ | 874 #endif // UI_EVENTS_EVENT_H_ |
OLD | NEW |