| 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 return static_cast<base::char16>(key_.ToCharacter()); |
| 814 return 0; |
| 842 } | 815 } |
| 843 | 816 |
| 844 base::char16 KeyEvent::GetText() const { | 817 base::char16 KeyEvent::GetText() const { |
| 845 if ((flags() & EF_CONTROL_DOWN) != 0) { | 818 if ((flags() & EF_CONTROL_DOWN) != 0) { |
| 846 base::char16 character; | |
| 847 ui::DomKey key; | 819 ui::DomKey key; |
| 848 ui::KeyboardCode key_code; | 820 ui::KeyboardCode key_code; |
| 849 if (DomCodeToControlCharacter(code_, flags(), &key, &character, &key_code)) | 821 if (DomCodeToControlCharacter(code_, flags(), &key, &key_code)) |
| 850 return character; | 822 return key.ToCharacter(); |
| 851 } | 823 } |
| 852 return GetUnmodifiedText(); | 824 return GetUnmodifiedText(); |
| 853 } | 825 } |
| 854 | 826 |
| 855 base::char16 KeyEvent::GetUnmodifiedText() const { | 827 base::char16 KeyEvent::GetUnmodifiedText() const { |
| 856 if (!is_char_ && (key_code_ == VKEY_RETURN)) | 828 if (!is_char_ && (key_code_ == VKEY_RETURN)) |
| 857 return '\r'; | 829 return '\r'; |
| 858 return GetCharacter(); | 830 return GetCharacter(); |
| 859 } | 831 } |
| 860 | 832 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 901 else | 873 else |
| 902 set_flags(flags() & ~mask); | 874 set_flags(flags() & ~mask); |
| 903 } | 875 } |
| 904 | 876 |
| 905 KeyboardCode KeyEvent::GetLocatedWindowsKeyboardCode() const { | 877 KeyboardCode KeyEvent::GetLocatedWindowsKeyboardCode() const { |
| 906 return NonLocatedToLocatedKeyboardCode(key_code_, code_); | 878 return NonLocatedToLocatedKeyboardCode(key_code_, code_); |
| 907 } | 879 } |
| 908 | 880 |
| 909 uint16 KeyEvent::GetConflatedWindowsKeyCode() const { | 881 uint16 KeyEvent::GetConflatedWindowsKeyCode() const { |
| 910 if (is_char_) | 882 if (is_char_) |
| 911 return character_; | 883 return key_.ToCharacter(); |
| 912 return key_code_; | 884 return key_code_; |
| 913 } | 885 } |
| 914 | 886 |
| 915 std::string KeyEvent::GetCodeString() const { | 887 std::string KeyEvent::GetCodeString() const { |
| 916 return KeycodeConverter::DomCodeToCodeString(code_); | 888 return KeycodeConverter::DomCodeToCodeString(code_); |
| 917 } | 889 } |
| 918 | 890 |
| 919 //////////////////////////////////////////////////////////////////////////////// | 891 //////////////////////////////////////////////////////////////////////////////// |
| 920 // ScrollEvent | 892 // ScrollEvent |
| 921 | 893 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 976 gfx::PointF(x, y), | 948 gfx::PointF(x, y), |
| 977 time_stamp, | 949 time_stamp, |
| 978 flags | EF_FROM_TOUCH), | 950 flags | EF_FROM_TOUCH), |
| 979 details_(details) { | 951 details_(details) { |
| 980 } | 952 } |
| 981 | 953 |
| 982 GestureEvent::~GestureEvent() { | 954 GestureEvent::~GestureEvent() { |
| 983 } | 955 } |
| 984 | 956 |
| 985 } // namespace ui | 957 } // namespace ui |
| OLD | NEW |