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 #include "ui/events/event.h" | 5 #include "ui/events/event.h" |
6 | 6 |
7 #if defined(USE_X11) | 7 #if defined(USE_X11) |
8 #include <X11/extensions/XInput2.h> | 8 #include <X11/extensions/XInput2.h> |
9 #include <X11/keysym.h> | 9 #include <X11/keysym.h> |
10 #include <X11/Xlib.h> | 10 #include <X11/Xlib.h> |
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 return false; | 666 return false; |
667 } | 667 } |
668 | 668 |
669 KeyEvent::KeyEvent(const base::NativeEvent& native_event) | 669 KeyEvent::KeyEvent(const base::NativeEvent& native_event) |
670 : Event(native_event, | 670 : Event(native_event, |
671 EventTypeFromNative(native_event), | 671 EventTypeFromNative(native_event), |
672 EventFlagsFromNative(native_event)), | 672 EventFlagsFromNative(native_event)), |
673 key_code_(KeyboardCodeFromNative(native_event)), | 673 key_code_(KeyboardCodeFromNative(native_event)), |
674 code_(CodeFromNative(native_event)), | 674 code_(CodeFromNative(native_event)), |
675 is_char_(IsCharFromNative(native_event)), | 675 is_char_(IsCharFromNative(native_event)), |
676 platform_keycode_(PlatformKeycodeFromNative(native_event)), | 676 platform_keycode_(PlatformKeycodeFromNative(native_event)) { |
677 key_(DomKey::NONE), | |
678 character_(0) { | |
679 if (IsRepeated(*this)) | 677 if (IsRepeated(*this)) |
680 set_flags(flags() | ui::EF_IS_REPEAT); | 678 set_flags(flags() | ui::EF_IS_REPEAT); |
681 | 679 |
682 #if defined(USE_X11) | 680 #if defined(USE_X11) |
683 NormalizeFlags(); | 681 NormalizeFlags(); |
684 #endif | 682 #endif |
685 #if defined(OS_WIN) | 683 #if defined(OS_WIN) |
686 // Only Windows has native character events. | 684 // Only Windows has native character events. |
687 if (is_char_) | 685 if (is_char_) |
688 character_ = native_event.wParam; | 686 key_ = DomKey::FromCharacter(native_event.wParam); |
689 #endif | 687 #endif |
690 } | 688 } |
691 | 689 |
692 KeyEvent::KeyEvent(EventType type, | 690 KeyEvent::KeyEvent(EventType type, |
693 KeyboardCode key_code, | 691 KeyboardCode key_code, |
694 int flags) | 692 int flags) |
695 : Event(type, EventTimeForNow(), flags), | 693 : Event(type, EventTimeForNow(), flags), |
696 key_code_(key_code), | 694 key_code_(key_code), |
697 code_(UsLayoutKeyboardCodeToDomCode(key_code)), | 695 code_(UsLayoutKeyboardCodeToDomCode(key_code)) { |
698 is_char_(false), | |
699 platform_keycode_(0), | |
700 key_(DomKey::NONE), | |
701 character_() { | |
702 } | 696 } |
703 | 697 |
704 KeyEvent::KeyEvent(EventType type, | 698 KeyEvent::KeyEvent(EventType type, |
705 KeyboardCode key_code, | 699 KeyboardCode key_code, |
706 DomCode code, | 700 DomCode code, |
707 int flags) | 701 int flags) |
708 : Event(type, EventTimeForNow(), flags), | 702 : Event(type, EventTimeForNow(), flags), |
709 key_code_(key_code), | 703 key_code_(key_code), |
710 code_(code), | 704 code_(code) { |
711 is_char_(false), | |
712 platform_keycode_(0), | |
713 key_(DomKey::NONE), | |
714 character_(0) { | |
715 } | 705 } |
716 | 706 |
717 KeyEvent::KeyEvent(EventType type, | 707 KeyEvent::KeyEvent(EventType type, |
718 KeyboardCode key_code, | 708 KeyboardCode key_code, |
719 DomCode code, | 709 DomCode code, |
720 int flags, | 710 int flags, |
721 DomKey key, | 711 DomKey key, |
722 base::char16 character, | |
723 base::TimeDelta time_stamp) | 712 base::TimeDelta time_stamp) |
724 : Event(type, time_stamp, flags), | 713 : Event(type, time_stamp, flags), |
725 key_code_(key_code), | 714 key_code_(key_code), |
726 code_(code), | 715 code_(code), |
727 is_char_(false), | 716 key_(key) { |
728 platform_keycode_(0), | |
729 key_(key), | |
730 character_(character) { | |
731 } | 717 } |
732 | 718 |
733 KeyEvent::KeyEvent(base::char16 character, KeyboardCode key_code, int flags) | 719 KeyEvent::KeyEvent(base::char16 character, KeyboardCode key_code, int flags) |
734 : Event(ET_KEY_PRESSED, EventTimeForNow(), flags), | 720 : Event(ET_KEY_PRESSED, EventTimeForNow(), flags), |
735 key_code_(key_code), | 721 key_code_(key_code), |
736 code_(DomCode::NONE), | 722 code_(DomCode::NONE), |
737 is_char_(true), | 723 is_char_(true), |
738 platform_keycode_(0), | 724 key_(DomKey::FromCharacter(character)) { |
739 key_(DomKey::CHARACTER), | |
740 character_(character) { | |
741 } | 725 } |
742 | 726 |
743 KeyEvent::KeyEvent(const KeyEvent& rhs) | 727 KeyEvent::KeyEvent(const KeyEvent& rhs) |
744 : Event(rhs), | 728 : Event(rhs), |
745 key_code_(rhs.key_code_), | 729 key_code_(rhs.key_code_), |
746 code_(rhs.code_), | 730 code_(rhs.code_), |
747 is_char_(rhs.is_char_), | 731 is_char_(rhs.is_char_), |
748 platform_keycode_(rhs.platform_keycode_), | 732 platform_keycode_(rhs.platform_keycode_), |
749 key_(rhs.key_), | 733 key_(rhs.key_) { |
750 character_(rhs.character_) { | |
751 if (rhs.extended_key_event_data_) | 734 if (rhs.extended_key_event_data_) |
752 extended_key_event_data_.reset(rhs.extended_key_event_data_->Clone()); | 735 extended_key_event_data_.reset(rhs.extended_key_event_data_->Clone()); |
753 } | 736 } |
754 | 737 |
755 KeyEvent& KeyEvent::operator=(const KeyEvent& rhs) { | 738 KeyEvent& KeyEvent::operator=(const KeyEvent& rhs) { |
756 if (this != &rhs) { | 739 if (this != &rhs) { |
757 Event::operator=(rhs); | 740 Event::operator=(rhs); |
758 key_code_ = rhs.key_code_; | 741 key_code_ = rhs.key_code_; |
759 code_ = rhs.code_; | 742 code_ = rhs.code_; |
760 key_ = rhs.key_; | 743 key_ = rhs.key_; |
761 is_char_ = rhs.is_char_; | 744 is_char_ = rhs.is_char_; |
762 platform_keycode_ = rhs.platform_keycode_; | 745 platform_keycode_ = rhs.platform_keycode_; |
763 character_ = rhs.character_; | |
764 | 746 |
765 if (rhs.extended_key_event_data_) | 747 if (rhs.extended_key_event_data_) |
766 extended_key_event_data_.reset(rhs.extended_key_event_data_->Clone()); | 748 extended_key_event_data_.reset(rhs.extended_key_event_data_->Clone()); |
767 } | 749 } |
768 return *this; | 750 return *this; |
769 } | 751 } |
770 | 752 |
771 KeyEvent::~KeyEvent() {} | 753 KeyEvent::~KeyEvent() {} |
772 | 754 |
773 void KeyEvent::SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data) { | 755 void KeyEvent::SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data) { |
774 extended_key_event_data_ = data.Pass(); | 756 extended_key_event_data_ = data.Pass(); |
775 } | 757 } |
776 | 758 |
777 void KeyEvent::ApplyLayout() const { | 759 void KeyEvent::ApplyLayout() const { |
778 // If the client has set the character (e.g. faked key events from virtual | |
779 // keyboard), it's client's responsibility to set the dom key correctly. | |
780 // Otherwise, set the dom key as unidentified. | |
781 // Please refer to crbug.com/443889. | |
782 if (character_ != 0) { | |
783 key_ = DomKey::UNIDENTIFIED; | |
784 return; | |
785 } | |
786 ui::DomCode code = code_; | 760 ui::DomCode code = code_; |
787 if (code == DomCode::NONE) { | 761 if (code == DomCode::NONE) { |
788 // Catch old code that tries to do layout without a physical key, and try | 762 // Catch old code that tries to do layout without a physical key, and try |
789 // to recover using the KeyboardCode. Once key events are fully defined | 763 // to recover using the KeyboardCode. Once key events are fully defined |
790 // on construction (see TODO in event.h) this will go away. | 764 // on construction (see TODO in event.h) this will go away. |
791 LOG(WARNING) << "DomCode::NONE keycode=" << key_code_; | 765 LOG(WARNING) << "DomCode::NONE keycode=" << key_code_; |
792 code = UsLayoutKeyboardCodeToDomCode(key_code_); | 766 code = UsLayoutKeyboardCodeToDomCode(key_code_); |
793 if (code == DomCode::NONE) { | 767 if (code == DomCode::NONE) { |
794 key_ = DomKey::UNIDENTIFIED; | 768 key_ = DomKey::UNIDENTIFIED; |
795 return; | 769 return; |
796 } | 770 } |
797 } | 771 } |
798 KeyboardCode dummy_key_code; | 772 KeyboardCode dummy_key_code; |
799 #if defined(OS_WIN) | 773 #if defined(OS_WIN) |
800 // Native Windows character events always have is_char_ == true, | 774 // Native Windows character events always have is_char_ == true, |
801 // so this is a synthetic or native keystroke event. | 775 // so this is a synthetic or native keystroke event. |
802 // Therefore, perform only the fallback action. | 776 // Therefore, perform only the fallback action. |
803 #elif defined(USE_X11) | 777 #elif defined(USE_X11) |
804 // When a control key is held, prefer ASCII characters to non ASCII | 778 // When a control key is held, prefer ASCII characters to non ASCII |
805 // characters in order to use it for shortcut keys. GetCharacterFromKeyCode | 779 // characters in order to use it for shortcut keys. GetCharacterFromKeyCode |
806 // returns 'a' for VKEY_A even if the key is actually bound to 'à' in X11. | 780 // returns 'a' for VKEY_A even if the key is actually bound to 'à' in X11. |
807 // GetCharacterFromXEvent returns 'à' in that case. | 781 // GetCharacterFromXEvent returns 'à' in that case. |
808 if (!IsControlDown() && native_event()) { | 782 if (!IsControlDown() && native_event()) { |
809 GetMeaningFromXEvent(native_event(), &key_, &character_); | 783 key_ = GetDomKeyFromXEvent(native_event()); |
810 return; | 784 return; |
811 } | 785 } |
812 #elif defined(USE_OZONE) | 786 #elif defined(USE_OZONE) |
813 if (KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()->Lookup( | 787 if (KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()->Lookup( |
814 code, flags(), &key_, &character_, &dummy_key_code, | 788 code, flags(), &key_, &dummy_key_code, &platform_keycode_)) { |
815 &platform_keycode_)) { | |
816 return; | 789 return; |
817 } | 790 } |
818 #else | 791 #else |
819 if (native_event()) { | 792 if (native_event()) { |
820 DCHECK(EventTypeFromNative(native_event()) == ET_KEY_PRESSED || | 793 DCHECK(EventTypeFromNative(native_event()) == ET_KEY_PRESSED || |
821 EventTypeFromNative(native_event()) == ET_KEY_RELEASED); | 794 EventTypeFromNative(native_event()) == ET_KEY_RELEASED); |
822 } | 795 } |
823 #endif | 796 #endif |
824 if (!DomCodeToUsLayoutMeaning(code, flags(), &key_, &character_, | 797 if (!DomCodeToUsLayoutDomKey(code, flags(), &key_, &dummy_key_code)) |
825 &dummy_key_code)) { | |
826 key_ = DomKey::UNIDENTIFIED; | 798 key_ = DomKey::UNIDENTIFIED; |
827 } | |
828 } | 799 } |
829 | 800 |
830 DomKey KeyEvent::GetDomKey() const { | 801 DomKey KeyEvent::GetDomKey() const { |
831 // Determination of character_ and key_ may be done lazily. | 802 // Determination of key_ may be done lazily. |
832 if (key_ == DomKey::NONE) | 803 if (key_ == DomKey::NONE) |
833 ApplyLayout(); | 804 ApplyLayout(); |
834 return key_; | 805 return key_; |
835 } | 806 } |
836 | 807 |
837 base::char16 KeyEvent::GetCharacter() const { | 808 base::char16 KeyEvent::GetCharacter() const { |
838 // Determination of character_ and key_ may be done lazily. | 809 // Determination of key_ may be done lazily. |
839 if (key_ == DomKey::NONE) | 810 if (key_ == DomKey::NONE) |
840 ApplyLayout(); | 811 ApplyLayout(); |
841 return character_; | 812 if (key_.IsCharacter()) { |
| 813 // Historically ui::KeyEvent has held only BMP characters. |
| 814 // Until this explicitly changes, require |key_| to hold a BMP character. |
| 815 DomKey::Base utf32_character = key_.ToCharacter(); |
| 816 base::char16 ucs2_character = static_cast<base::char16>(utf32_character); |
| 817 DCHECK(static_cast<DomKey::Base>(ucs2_character) == utf32_character); |
| 818 return ucs2_character; |
| 819 } |
| 820 return 0; |
842 } | 821 } |
843 | 822 |
844 base::char16 KeyEvent::GetText() const { | 823 base::char16 KeyEvent::GetText() const { |
845 if ((flags() & EF_CONTROL_DOWN) != 0) { | 824 if ((flags() & EF_CONTROL_DOWN) != 0) { |
846 base::char16 character; | |
847 ui::DomKey key; | 825 ui::DomKey key; |
848 ui::KeyboardCode key_code; | 826 ui::KeyboardCode key_code; |
849 if (DomCodeToControlCharacter(code_, flags(), &key, &character, &key_code)) | 827 if (DomCodeToControlCharacter(code_, flags(), &key, &key_code)) |
850 return character; | 828 return key.ToCharacter(); |
851 } | 829 } |
852 return GetUnmodifiedText(); | 830 return GetUnmodifiedText(); |
853 } | 831 } |
854 | 832 |
855 base::char16 KeyEvent::GetUnmodifiedText() const { | 833 base::char16 KeyEvent::GetUnmodifiedText() const { |
856 if (!is_char_ && (key_code_ == VKEY_RETURN)) | 834 if (!is_char_ && (key_code_ == VKEY_RETURN)) |
857 return '\r'; | 835 return '\r'; |
858 return GetCharacter(); | 836 return GetCharacter(); |
859 } | 837 } |
860 | 838 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
901 else | 879 else |
902 set_flags(flags() & ~mask); | 880 set_flags(flags() & ~mask); |
903 } | 881 } |
904 | 882 |
905 KeyboardCode KeyEvent::GetLocatedWindowsKeyboardCode() const { | 883 KeyboardCode KeyEvent::GetLocatedWindowsKeyboardCode() const { |
906 return NonLocatedToLocatedKeyboardCode(key_code_, code_); | 884 return NonLocatedToLocatedKeyboardCode(key_code_, code_); |
907 } | 885 } |
908 | 886 |
909 uint16 KeyEvent::GetConflatedWindowsKeyCode() const { | 887 uint16 KeyEvent::GetConflatedWindowsKeyCode() const { |
910 if (is_char_) | 888 if (is_char_) |
911 return character_; | 889 return key_.ToCharacter(); |
912 return key_code_; | 890 return key_code_; |
913 } | 891 } |
914 | 892 |
915 std::string KeyEvent::GetCodeString() const { | 893 std::string KeyEvent::GetCodeString() const { |
916 return KeycodeConverter::DomCodeToCodeString(code_); | 894 return KeycodeConverter::DomCodeToCodeString(code_); |
917 } | 895 } |
918 | 896 |
919 //////////////////////////////////////////////////////////////////////////////// | 897 //////////////////////////////////////////////////////////////////////////////// |
920 // ScrollEvent | 898 // ScrollEvent |
921 | 899 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
976 gfx::PointF(x, y), | 954 gfx::PointF(x, y), |
977 time_stamp, | 955 time_stamp, |
978 flags | EF_FROM_TOUCH), | 956 flags | EF_FROM_TOUCH), |
979 details_(details) { | 957 details_(details) { |
980 } | 958 } |
981 | 959 |
982 GestureEvent::~GestureEvent() { | 960 GestureEvent::~GestureEvent() { |
983 } | 961 } |
984 | 962 |
985 } // namespace ui | 963 } // namespace ui |
OLD | NEW |