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/dom/dom_key.h" |
18 #include "ui/events/keycodes/keyboard_codes.h" | 19 #include "ui/events/keycodes/keyboard_codes.h" |
19 #include "ui/events/latency_info.h" | 20 #include "ui/events/latency_info.h" |
20 #include "ui/gfx/geometry/point.h" | 21 #include "ui/gfx/geometry/point.h" |
21 #include "ui/gfx/geometry/point_conversions.h" | 22 #include "ui/gfx/geometry/point_conversions.h" |
22 | 23 |
23 namespace gfx { | 24 namespace gfx { |
24 class Transform; | 25 class Transform; |
25 } | 26 } |
26 | 27 |
27 namespace ui { | 28 namespace ui { |
28 class EventTarget; | 29 class EventTarget; |
29 enum class DomCode; | 30 enum class DomCode; |
30 enum class DomKey; | |
31 | 31 |
32 class EVENTS_EXPORT Event { | 32 class EVENTS_EXPORT Event { |
33 public: | 33 public: |
34 static scoped_ptr<Event> Clone(const Event& event); | 34 static scoped_ptr<Event> Clone(const Event& event); |
35 | 35 |
36 virtual ~Event(); | 36 virtual ~Event(); |
37 | 37 |
38 class DispatcherApi { | 38 class DispatcherApi { |
39 public: | 39 public: |
40 explicit DispatcherApi(Event* event) : event_(event) {} | 40 explicit DispatcherApi(Event* event) : event_(event) {} |
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 virtual ~ExtendedKeyEventData() {} | 652 virtual ~ExtendedKeyEventData() {} |
653 | 653 |
654 virtual ExtendedKeyEventData* Clone() const = 0; | 654 virtual ExtendedKeyEventData* Clone() const = 0; |
655 }; | 655 }; |
656 | 656 |
657 // A KeyEvent is really two distinct classes, melded together due to the | 657 // A KeyEvent is really two distinct classes, melded together due to the |
658 // DOM legacy of Windows key events: a keystroke event (is_char_ == false), | 658 // DOM legacy of Windows key events: a keystroke event (is_char_ == false), |
659 // or a character event (is_char_ == true). | 659 // or a character event (is_char_ == true). |
660 // | 660 // |
661 // For a keystroke event, | 661 // For a keystroke event, |
662 // -- is_char_ is false. | 662 // -- |bool is_char_| is false. |
663 // -- Event::type() can be any one of ET_KEY_PRESSED, ET_KEY_RELEASED. | 663 // -- |EventType Event::type()| can be ET_KEY_PRESSED or ET_KEY_RELEASED. |
664 // -- code_ and Event::flags() represent the physical key event. | 664 // -- |DomCode code_| and |int Event::flags()| represent the physical key event. |
665 // - code_ is a platform-independent representation of the physical key, | 665 // - code_ is a platform-independent representation of the physical key, |
666 // based on DOM KeyboardEvent |code| values. It does not vary depending | 666 // based on DOM UI Events KeyboardEvent |code| values. It does not |
667 // on key layout. | 667 // vary depending on key layout. |
| 668 // http://www.w3.org/TR/DOM-Level-3-Events-code/ |
668 // - Event::flags() provides the active modifiers for the physical key | 669 // - Event::flags() provides the active modifiers for the physical key |
669 // press. Its value reflects the state after the event; that is, for | 670 // press. Its value reflects the state after the event; that is, for |
670 // a modifier key, a press includes the corresponding flag and a release | 671 // a modifier key, a press includes the corresponding flag and a release |
671 // does not. | 672 // does not. |
672 // -- key_ and character_ provide the meaning of the key event, in the context | 673 // -- |DomKey key_| provides the meaning (character or action) of the key |
673 // of the active layout and modifiers. Together they correspond to DOM | 674 // event, in the context of the active layout and modifiers. It corresponds |
674 // KeyboardEvent |key| values. | 675 // to DOM UI Events KeyboardEvent |key| values. |
675 // - key_ is an enumeration of non-Unicode meanings, plus sentinels | 676 // http://www.w3.org/TR/DOM-Level-3-Events-key/ |
676 // (specifically DomKey::CHARACTER for Unicode meanings). | 677 // -- |KeyboardCode key_code_| supports the legacy web event |keyCode| field, |
677 // - character_ is the code point for Unicode meanings. | 678 // and its VKEY_ values are chosen to match Windows/IE for compatibility. |
678 // -- key_code_ is a KeyboardCode value associated with the key. This supports | 679 // For printable characters, this may or may not be a layout-mapped value, |
679 // the legacy web event |keyCode| field, and the VKEY_ values are chosen | 680 // imitating MS Windows: if the mapped key generates a character that has |
680 // to match Windows/IE for compatibility. For printable characters, this | 681 // an associated VKEY_ code, then key_code_ is that code; if not, then |
681 // may or may not be a layout-mapped value, imitating MS Windows: | 682 // key_code_ is the unmapped VKEY_ code. For example, US, Greek, Cyrillic, |
682 // if the mapped key generates a character that has an associated VKEY_ | 683 // Japanese, etc. all use VKEY_Q for the key beside Tab, while French uses |
683 // code, then key_code_ is that code; if not, then key_code_ is the unmapped | 684 // VKEY_A. The stored key_code_ is non-located (e.g. VKEY_SHIFT rather than |
684 // VKEY_ code. For example, US, Greek, Cyrillic, Japanese, etc. all use | 685 // VKEY_LSHIFT, VKEY_1 rather than VKEY_NUMPAD1). |
685 // VKEY_Q for the key beside Tab, while French uses VKEY_A. The stored | |
686 // key_code_ is non-located (e.g. VKEY_SHIFT rather than VKEY_LSHIFT, | |
687 // VKEY_1 rather than VKEY_NUMPAD1). | |
688 // | 686 // |
689 // For a character event, | 687 // For a character event, |
690 // -- is_char_ is true. | 688 // -- |bool is_char_| is true. |
691 // -- type() is ET_KEY_PRESSED. | 689 // -- |EventType Event::type()| is ET_KEY_PRESSED. |
692 // -- code_ is DomCode::NONE. | 690 // -- |DomCode code_| is DomCode::NONE. |
693 // -- key_ is DomKey::CHARACTER and character_ is a UTF-16 code point. | 691 // -- |DomKey key_| is a UTF-16 code point. |
694 // -- key_code_ is conflated with character_ by some code, because both | 692 // -- |KeyboardCode key_code_| is conflated with the character-valued key_ |
695 // arrive in the wParam field of a Windows event. | 693 // by some code, because both arrive in the wParam field of a Windows event. |
696 // | 694 // |
697 class EVENTS_EXPORT KeyEvent : public Event { | 695 class EVENTS_EXPORT KeyEvent : public Event { |
698 public: | 696 public: |
699 // Create a KeyEvent from a NativeEvent. For Windows this native event can | 697 // Create a KeyEvent from a NativeEvent. For Windows this native event can |
700 // be either a keystroke message (WM_KEYUP/WM_KEYDOWN) or a character message | 698 // be either a keystroke message (WM_KEYUP/WM_KEYDOWN) or a character message |
701 // (WM_CHAR). Other systems have only keystroke events. | 699 // (WM_CHAR). Other systems have only keystroke events. |
702 explicit KeyEvent(const base::NativeEvent& native_event); | 700 explicit KeyEvent(const base::NativeEvent& native_event); |
703 | 701 |
704 // Create a keystroke event. | 702 // Create a keystroke event from a legacy KeyboardCode. |
| 703 // This should not be used in new code. |
705 KeyEvent(EventType type, KeyboardCode key_code, int flags); | 704 KeyEvent(EventType type, KeyboardCode key_code, int flags); |
706 | 705 |
707 // Create a fully defined keystroke event. | 706 // Create a fully defined keystroke event. |
708 KeyEvent(EventType type, | 707 KeyEvent(EventType type, |
709 KeyboardCode key_code, | 708 KeyboardCode key_code, |
710 DomCode code, | 709 DomCode code, |
711 int flags, | 710 int flags, |
712 DomKey key, | 711 DomKey key, |
713 base::char16 character, | |
714 base::TimeDelta time_stamp); | 712 base::TimeDelta time_stamp); |
715 | 713 |
716 // Create a character event. | 714 // Create a character event. |
717 KeyEvent(base::char16 character, KeyboardCode key_code, int flags); | 715 KeyEvent(base::char16 character, KeyboardCode key_code, int flags); |
718 | 716 |
719 // Used for synthetic events with code of DOM KeyboardEvent (e.g. 'KeyA') | 717 // Used for synthetic events with code of DOM KeyboardEvent (e.g. 'KeyA') |
720 // See also: ui/events/keycodes/dom/dom_values.txt | 718 // See also: ui/events/keycodes/dom/dom_values.txt |
721 KeyEvent(EventType type, | 719 KeyEvent(EventType type, |
722 KeyboardCode key_code, | 720 KeyboardCode key_code, |
723 DomCode code, | 721 DomCode code, |
(...skipping 13 matching lines...) Expand all Loading... |
737 // resulting data when read across IPC boundaries. | 735 // resulting data when read across IPC boundaries. |
738 void SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data); | 736 void SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data); |
739 const ExtendedKeyEventData* extended_key_event_data() const { | 737 const ExtendedKeyEventData* extended_key_event_data() const { |
740 return extended_key_event_data_.get(); | 738 return extended_key_event_data_.get(); |
741 } | 739 } |
742 | 740 |
743 // This bypasses the normal mapping from keystroke events to characters, | 741 // This bypasses the normal mapping from keystroke events to characters, |
744 // which allows an I18N virtual keyboard to fabricate a keyboard event that | 742 // which allows an I18N virtual keyboard to fabricate a keyboard event that |
745 // does not have a corresponding KeyboardCode (example: U+00E1 Latin small | 743 // does not have a corresponding KeyboardCode (example: U+00E1 Latin small |
746 // letter A with acute, U+0410 Cyrillic capital letter A). | 744 // letter A with acute, U+0410 Cyrillic capital letter A). |
747 void set_character(base::char16 character) { character_ = character; } | 745 void set_character(base::char16 character) { |
| 746 key_ = DomKey::FromCharacter(character); |
| 747 } |
748 | 748 |
749 // Gets the character generated by this key event. It only supports Unicode | 749 // Gets the character generated by this key event. It only supports Unicode |
750 // BMP characters. | 750 // BMP characters. |
751 base::char16 GetCharacter() const; | 751 base::char16 GetCharacter() const; |
752 | 752 |
753 // If this is a keystroke event with key_code_ VKEY_RETURN, returns '\r'; | 753 // If this is a keystroke event with key_code_ VKEY_RETURN, returns '\r'; |
754 // otherwise returns the same as GetCharacter(). | 754 // otherwise returns the same as GetCharacter(). |
755 base::char16 GetUnmodifiedText() const; | 755 base::char16 GetUnmodifiedText() const; |
756 | 756 |
757 // If the Control key is down in the event, returns a layout-independent | 757 // If the Control key is down in the event, returns a layout-independent |
758 // character (corresponding to US layout); otherwise returns the same | 758 // character (corresponding to US layout); otherwise returns the same |
759 // as GetUnmodifiedText(). | 759 // as GetUnmodifiedText(). |
760 base::char16 GetText() const; | 760 base::char16 GetText() const; |
761 | 761 |
762 // Gets the platform key code. For XKB, this is the xksym value. | 762 // Gets the platform key code. For XKB, this is the xksym value. |
| 763 // This should not be used in new code. |
763 void set_platform_keycode(uint32 keycode) { platform_keycode_ = keycode; } | 764 void set_platform_keycode(uint32 keycode) { platform_keycode_ = keycode; } |
764 uint32 platform_keycode() const { return platform_keycode_; } | 765 uint32 platform_keycode() const { return platform_keycode_; } |
765 | 766 |
766 // Gets the associated (Windows-based) KeyboardCode for this key event. | 767 // Gets the associated (Windows-based) KeyboardCode for this key event. |
767 // Historically, this has also been used to obtain the character associated | 768 // Historically, this has also been used to obtain the character associated |
768 // with a character event, because both use the Window message 'wParam' field. | 769 // with a character event, because both use the Window message 'wParam' field. |
769 // This should be avoided; if necessary for backwards compatibility, use | 770 // This should be avoided; if necessary for backwards compatibility, use |
770 // GetConflatedWindowsKeyCode(). | 771 // GetConflatedWindowsKeyCode(). |
771 KeyboardCode key_code() const { return key_code_; } | 772 KeyboardCode key_code() const { return key_code_; } |
772 | 773 |
(...skipping 30 matching lines...) Expand all Loading... |
803 // (Native X11 event flags describe the state before the event.) | 804 // (Native X11 event flags describe the state before the event.) |
804 void NormalizeFlags(); | 805 void NormalizeFlags(); |
805 | 806 |
806 protected: | 807 protected: |
807 friend class KeyEventTestApi; | 808 friend class KeyEventTestApi; |
808 | 809 |
809 // This allows a subclass TranslatedKeyEvent to be a non character event. | 810 // This allows a subclass TranslatedKeyEvent to be a non character event. |
810 void set_is_char(bool is_char) { is_char_ = is_char; } | 811 void set_is_char(bool is_char) { is_char_ = is_char; } |
811 | 812 |
812 private: | 813 private: |
813 // Determine key_ and character_ on a keystroke event from code_ and flags(). | 814 // Determine key_ on a keystroke event from code_ and flags(). |
814 void ApplyLayout() const; | 815 void ApplyLayout() const; |
815 | 816 |
816 KeyboardCode key_code_; | 817 KeyboardCode key_code_; |
817 | 818 |
818 // DOM KeyboardEvent |code| (e.g. DomCode::KEY_A, DomCode::SPACE). | 819 // DOM KeyboardEvent |code| (e.g. DomCode::KEY_A, DomCode::SPACE). |
819 // http://www.w3.org/TR/DOM-Level-3-Events-code/ | 820 // http://www.w3.org/TR/DOM-Level-3-Events-code/ |
820 // | 821 // |
821 // This value represents the physical position in the keyboard and can be | 822 // This value represents the physical position in the keyboard and can be |
822 // converted from / to keyboard scan code like XKB. | 823 // converted from / to keyboard scan code like XKB. |
823 DomCode code_; | 824 DomCode code_; |
824 | 825 |
825 // True if this is a character event, false if this is a keystroke event. | 826 // True if this is a character event, false if this is a keystroke event. |
826 bool is_char_; | 827 bool is_char_ = false; |
827 | 828 |
828 // The platform related keycode value. For XKB, it's keysym value. | 829 // The platform related keycode value. For XKB, it's keysym value. |
829 // For now, this is used for CharacterComposer in ChromeOS. | 830 // For now, this is used for CharacterComposer in ChromeOS. |
830 mutable uint32 platform_keycode_; | 831 mutable uint32 platform_keycode_ = 0; |
831 | 832 |
832 // TODO(kpschoedel): refactor so that key_ and character_ are not mutable. | 833 // TODO(kpschoedel): refactor so that key_ is not mutable. |
833 // This requires defining the KeyEvent completely at construction rather | 834 // This requires defining the KeyEvent completely at construction rather |
834 // than lazily under GetCharacter(), which likely also means removing | 835 // than lazily under GetCharacter(), which likely also means removing |
835 // the two 'incomplete' constructors. crbug.com/444045 | 836 // the two 'incomplete' constructors. crbug.com/444045 |
836 // | 837 // |
837 // DOM KeyboardEvent |key| | 838 // DOM KeyboardEvent |key| |
838 // http://www.w3.org/TR/DOM-Level-3-Events-key/ | 839 // http://www.w3.org/TR/DOM-Level-3-Events-key/ |
839 // | 840 // |
840 // This value, together with character_, represents the meaning of a key. | 841 // This value represents the meaning of a key, which is either a Unicode |
841 // The value is DomKey::CHARACTER when the interpretation is a character. | 842 // character, or a named DomKey:: value. |
842 // This, along with character_, is not necessarily initialized when the | 843 // This is not necessarily initialized when the event is constructed; |
843 // event is constructed; it may be set only if and when GetCharacter() | 844 // it may be set only if and when GetCharacter() or GetDomKey() is called. |
844 // or GetDomKey() is called. | 845 mutable DomKey key_ = DomKey::NONE; |
845 mutable DomKey key_; | |
846 | |
847 // String of 'key' defined in DOM KeyboardEvent (e.g. 'a', 'รข') | |
848 // http://www.w3.org/TR/uievents/#keyboard-key-codes. | |
849 // | |
850 // This value represents the text that the key event will insert to input | |
851 // field. For key with modifier key, it may have specifial text. | |
852 // e.g. CTRL+A has '\x01'. | |
853 mutable base::char16 character_; | |
854 | 846 |
855 // Parts of our event handling require raw native events (see both the | 847 // Parts of our event handling require raw native events (see both the |
856 // windows and linux implementations of web_input_event in content/). Because | 848 // windows and linux implementations of web_input_event in content/). Because |
857 // mojo instead serializes and deserializes events in potentially different | 849 // mojo instead serializes and deserializes events in potentially different |
858 // processes, we need to have a mechanism to keep track of this data. | 850 // processes, we need to have a mechanism to keep track of this data. |
859 scoped_ptr<ExtendedKeyEventData> extended_key_event_data_; | 851 scoped_ptr<ExtendedKeyEventData> extended_key_event_data_; |
860 | 852 |
861 static bool IsRepeated(const KeyEvent& event); | 853 static bool IsRepeated(const KeyEvent& event); |
862 | 854 |
863 static KeyEvent* last_key_event_; | 855 static KeyEvent* last_key_event_; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
932 | 924 |
933 const GestureEventDetails& details() const { return details_; } | 925 const GestureEventDetails& details() const { return details_; } |
934 | 926 |
935 private: | 927 private: |
936 GestureEventDetails details_; | 928 GestureEventDetails details_; |
937 }; | 929 }; |
938 | 930 |
939 } // namespace ui | 931 } // namespace ui |
940 | 932 |
941 #endif // UI_EVENTS_EVENT_H_ | 933 #endif // UI_EVENTS_EVENT_H_ |
OLD | NEW |