Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: ui/events/event.cc

Issue 1284433002: Revise ui::DomKey to unify character and non-character codes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 653 matching lines...) Expand 10 before | Expand all | Expand 10 after
664 } 664 }
665 665
666 KeyEvent::KeyEvent(const base::NativeEvent& native_event) 666 KeyEvent::KeyEvent(const base::NativeEvent& native_event)
667 : Event(native_event, 667 : Event(native_event,
668 EventTypeFromNative(native_event), 668 EventTypeFromNative(native_event),
669 EventFlagsFromNative(native_event)), 669 EventFlagsFromNative(native_event)),
670 key_code_(KeyboardCodeFromNative(native_event)), 670 key_code_(KeyboardCodeFromNative(native_event)),
671 code_(CodeFromNative(native_event)), 671 code_(CodeFromNative(native_event)),
672 is_char_(IsCharFromNative(native_event)), 672 is_char_(IsCharFromNative(native_event)),
673 platform_keycode_(PlatformKeycodeFromNative(native_event)), 673 platform_keycode_(PlatformKeycodeFromNative(native_event)),
674 key_(DomKey::NONE), 674 key_() {
Wez 2015/08/13 22:31:48 No need to initialize |key_| if it takes no ctor p
kpschoedel 2015/08/17 20:16:14 Done.
675 character_(0) {
676 if (IsRepeated(*this)) 675 if (IsRepeated(*this))
677 set_flags(flags() | ui::EF_IS_REPEAT); 676 set_flags(flags() | ui::EF_IS_REPEAT);
678 677
679 #if defined(USE_X11) 678 #if defined(USE_X11)
680 NormalizeFlags(); 679 NormalizeFlags();
681 #endif 680 #endif
682 #if defined(OS_WIN) 681 #if defined(OS_WIN)
683 // Only Windows has native character events. 682 // Only Windows has native character events.
684 if (is_char_) 683 if (is_char_)
685 character_ = native_event.wParam; 684 key_ = DomKey(native_event.wParam);
686 #endif 685 #endif
687 } 686 }
688 687
689 KeyEvent::KeyEvent(EventType type, 688 KeyEvent::KeyEvent(EventType type,
690 KeyboardCode key_code, 689 KeyboardCode key_code,
691 int flags) 690 int flags)
692 : Event(type, EventTimeForNow(), flags), 691 : Event(type, EventTimeForNow(), flags),
693 key_code_(key_code), 692 key_code_(key_code),
694 code_(UsLayoutKeyboardCodeToDomCode(key_code)), 693 code_(UsLayoutKeyboardCodeToDomCode(key_code)),
695 is_char_(false), 694 is_char_(false),
696 platform_keycode_(0), 695 platform_keycode_(0),
Wez 2015/08/13 22:31:48 Could any of these fields have their initial value
kpschoedel 2015/08/17 20:16:14 Yes, this hasn't been touched since inline initial
Wez 2015/08/18 22:34:32 Acknowledged.
697 key_(DomKey::NONE), 696 key_() {
698 character_() {
699 } 697 }
700 698
701 KeyEvent::KeyEvent(EventType type, 699 KeyEvent::KeyEvent(EventType type,
702 KeyboardCode key_code, 700 KeyboardCode key_code,
703 DomCode code, 701 DomCode code,
704 int flags) 702 int flags)
705 : Event(type, EventTimeForNow(), flags), 703 : Event(type, EventTimeForNow(), flags),
706 key_code_(key_code), 704 key_code_(key_code),
707 code_(code), 705 code_(code),
708 is_char_(false), 706 is_char_(false),
709 platform_keycode_(0), 707 platform_keycode_(0),
710 key_(DomKey::NONE), 708 key_() {
711 character_(0) {
712 } 709 }
713 710
714 KeyEvent::KeyEvent(EventType type, 711 KeyEvent::KeyEvent(EventType type,
715 KeyboardCode key_code, 712 KeyboardCode key_code,
716 DomCode code, 713 DomCode code,
717 int flags, 714 int flags,
718 DomKey key, 715 DomKey key,
719 base::char16 character,
720 base::TimeDelta time_stamp) 716 base::TimeDelta time_stamp)
721 : Event(type, time_stamp, flags), 717 : Event(type, time_stamp, flags),
722 key_code_(key_code), 718 key_code_(key_code),
723 code_(code), 719 code_(code),
724 is_char_(false), 720 is_char_(false),
725 platform_keycode_(0), 721 platform_keycode_(0),
726 key_(key), 722 key_(key) {
727 character_(character) {
728 } 723 }
729 724
730 KeyEvent::KeyEvent(base::char16 character, KeyboardCode key_code, int flags) 725 KeyEvent::KeyEvent(base::char16 character, KeyboardCode key_code, int flags)
731 : Event(ET_KEY_PRESSED, EventTimeForNow(), flags), 726 : Event(ET_KEY_PRESSED, EventTimeForNow(), flags),
732 key_code_(key_code), 727 key_code_(key_code),
733 code_(DomCode::NONE), 728 code_(DomCode::NONE),
734 is_char_(true), 729 is_char_(true),
735 platform_keycode_(0), 730 platform_keycode_(0),
736 key_(DomKey::CHARACTER), 731 key_(character) {
737 character_(character) {
738 } 732 }
739 733
740 KeyEvent::KeyEvent(const KeyEvent& rhs) 734 KeyEvent::KeyEvent(const KeyEvent& rhs)
741 : Event(rhs), 735 : Event(rhs),
742 key_code_(rhs.key_code_), 736 key_code_(rhs.key_code_),
743 code_(rhs.code_), 737 code_(rhs.code_),
744 is_char_(rhs.is_char_), 738 is_char_(rhs.is_char_),
745 platform_keycode_(rhs.platform_keycode_), 739 platform_keycode_(rhs.platform_keycode_),
746 key_(rhs.key_), 740 key_(rhs.key_) {
747 character_(rhs.character_) {
748 if (rhs.extended_key_event_data_) 741 if (rhs.extended_key_event_data_)
749 extended_key_event_data_.reset(rhs.extended_key_event_data_->Clone()); 742 extended_key_event_data_.reset(rhs.extended_key_event_data_->Clone());
750 } 743 }
751 744
752 KeyEvent& KeyEvent::operator=(const KeyEvent& rhs) { 745 KeyEvent& KeyEvent::operator=(const KeyEvent& rhs) {
753 if (this != &rhs) { 746 if (this != &rhs) {
754 Event::operator=(rhs); 747 Event::operator=(rhs);
755 key_code_ = rhs.key_code_; 748 key_code_ = rhs.key_code_;
756 code_ = rhs.code_; 749 code_ = rhs.code_;
757 key_ = rhs.key_; 750 key_ = rhs.key_;
758 is_char_ = rhs.is_char_; 751 is_char_ = rhs.is_char_;
759 platform_keycode_ = rhs.platform_keycode_; 752 platform_keycode_ = rhs.platform_keycode_;
760 character_ = rhs.character_;
761 753
762 if (rhs.extended_key_event_data_) 754 if (rhs.extended_key_event_data_)
763 extended_key_event_data_.reset(rhs.extended_key_event_data_->Clone()); 755 extended_key_event_data_.reset(rhs.extended_key_event_data_->Clone());
764 } 756 }
765 return *this; 757 return *this;
766 } 758 }
767 759
768 KeyEvent::~KeyEvent() {} 760 KeyEvent::~KeyEvent() {}
769 761
770 void KeyEvent::SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data) { 762 void KeyEvent::SetExtendedKeyEventData(scoped_ptr<ExtendedKeyEventData> data) {
771 extended_key_event_data_ = data.Pass(); 763 extended_key_event_data_ = data.Pass();
772 } 764 }
773 765
774 void KeyEvent::ApplyLayout() const { 766 void KeyEvent::ApplyLayout() const {
775 // If the client has set the character (e.g. faked key events from virtual
776 // keyboard), it's client's responsibility to set the dom key correctly.
777 // Otherwise, set the dom key as unidentified.
778 // Please refer to crbug.com/443889.
779 if (character_ != 0) {
780 key_ = DomKey::UNIDENTIFIED;
781 return;
782 }
783 ui::DomCode code = code_; 767 ui::DomCode code = code_;
784 if (code == DomCode::NONE) { 768 if (code == DomCode::NONE) {
785 // Catch old code that tries to do layout without a physical key, and try 769 // Catch old code that tries to do layout without a physical key, and try
786 // to recover using the KeyboardCode. Once key events are fully defined 770 // to recover using the KeyboardCode. Once key events are fully defined
787 // on construction (see TODO in event.h) this will go away. 771 // on construction (see TODO in event.h) this will go away.
788 LOG(WARNING) << "DomCode::NONE keycode=" << key_code_; 772 LOG(WARNING) << "DomCode::NONE keycode=" << key_code_;
789 code = UsLayoutKeyboardCodeToDomCode(key_code_); 773 code = UsLayoutKeyboardCodeToDomCode(key_code_);
790 if (code == DomCode::NONE) { 774 if (code == DomCode::NONE) {
791 key_ = DomKey::UNIDENTIFIED; 775 key_ = DomKey::UNIDENTIFIED;
792 return; 776 return;
793 } 777 }
794 } 778 }
795 KeyboardCode dummy_key_code; 779 KeyboardCode dummy_key_code;
796 #if defined(OS_WIN) 780 #if defined(OS_WIN)
797 // Native Windows character events always have is_char_ == true, 781 // Native Windows character events always have is_char_ == true,
798 // so this is a synthetic or native keystroke event. 782 // so this is a synthetic or native keystroke event.
799 // Therefore, perform only the fallback action. 783 // Therefore, perform only the fallback action.
800 #elif defined(USE_X11) 784 #elif defined(USE_X11)
801 // When a control key is held, prefer ASCII characters to non ASCII 785 // When a control key is held, prefer ASCII characters to non ASCII
802 // characters in order to use it for shortcut keys. GetCharacterFromKeyCode 786 // characters in order to use it for shortcut keys. GetCharacterFromKeyCode
803 // returns 'a' for VKEY_A even if the key is actually bound to 'à' in X11. 787 // returns 'a' for VKEY_A even if the key is actually bound to 'à' in X11.
804 // GetCharacterFromXEvent returns 'à' in that case. 788 // GetCharacterFromXEvent returns 'à' in that case.
805 if (!IsControlDown() && native_event()) { 789 if (!IsControlDown() && native_event()) {
806 GetMeaningFromXEvent(native_event(), &key_, &character_); 790 key_ = GetDomKeyFromXEvent(native_event());
807 return; 791 return;
808 } 792 }
809 #elif defined(USE_OZONE) 793 #elif defined(USE_OZONE)
810 if (KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()->Lookup( 794 if (KeyboardLayoutEngineManager::GetKeyboardLayoutEngine()->Lookup(
811 code, flags(), &key_, &character_, &dummy_key_code, 795 code, flags(), &key_, &dummy_key_code, &platform_keycode_)) {
812 &platform_keycode_)) {
813 return; 796 return;
814 } 797 }
815 #else 798 #else
816 if (native_event()) { 799 if (native_event()) {
817 DCHECK(EventTypeFromNative(native_event()) == ET_KEY_PRESSED || 800 DCHECK(EventTypeFromNative(native_event()) == ET_KEY_PRESSED ||
818 EventTypeFromNative(native_event()) == ET_KEY_RELEASED); 801 EventTypeFromNative(native_event()) == ET_KEY_RELEASED);
819 } 802 }
820 #endif 803 #endif
821 if (!DomCodeToUsLayoutMeaning(code, flags(), &key_, &character_, 804 if (!DomCodeToUsLayoutDomKey(code, flags(), &key_, &dummy_key_code))
822 &dummy_key_code)) {
823 key_ = DomKey::UNIDENTIFIED; 805 key_ = DomKey::UNIDENTIFIED;
824 }
825 } 806 }
826 807
827 DomKey KeyEvent::GetDomKey() const { 808 DomKey KeyEvent::GetDomKey() const {
828 // Determination of character_ and key_ may be done lazily. 809 // Determination of key_ may be done lazily.
829 if (key_ == DomKey::NONE) 810 if (key_ == DomKey::NONE)
830 ApplyLayout(); 811 ApplyLayout();
831 return key_; 812 return key_;
832 } 813 }
833 814
834 base::char16 KeyEvent::GetCharacter() const { 815 base::char16 KeyEvent::GetCharacter() const {
kpschoedel 2015/08/07 20:42:12 We still need a separate GetCharacter() for cases
Wez 2015/08/13 22:31:48 Acknowledged.
835 // Determination of character_ and key_ may be done lazily. 816 // Determination of key_ may be done lazily.
836 if (key_ == DomKey::NONE) 817 if (key_ == DomKey::NONE)
837 ApplyLayout(); 818 ApplyLayout();
838 return character_; 819 if (key_.IsUnicode())
820 return key_;
dtapuska 2015/08/13 14:47:25 I find this tricky.. In that the code assumes that
kpschoedel 2015/08/13 15:46:06 This proposal is for a DomKey that is explicitly d
Wez 2015/08/13 22:31:48 I'd be fine with something like DomKey::ToChar(key
kpschoedel 2015/08/17 20:16:14 I haven't looked into it, but I assume builds use
Wez 2015/08/26 23:38:33 Acknowledged.
821 return 0;
839 } 822 }
840 823
841 base::char16 KeyEvent::GetText() const { 824 base::char16 KeyEvent::GetText() const {
842 if ((flags() & EF_CONTROL_DOWN) != 0) { 825 if ((flags() & EF_CONTROL_DOWN) != 0) {
843 base::char16 character;
844 ui::DomKey key; 826 ui::DomKey key;
845 ui::KeyboardCode key_code; 827 ui::KeyboardCode key_code;
846 if (DomCodeToControlCharacter(code_, flags(), &key, &character, &key_code)) 828 if (DomCodeToControlCharacter(code_, flags(), &key, &key_code))
847 return character; 829 return key;
848 } 830 }
849 return GetUnmodifiedText(); 831 return GetUnmodifiedText();
850 } 832 }
851 833
852 base::char16 KeyEvent::GetUnmodifiedText() const { 834 base::char16 KeyEvent::GetUnmodifiedText() const {
853 if (!is_char_ && (key_code_ == VKEY_RETURN)) 835 if (!is_char_ && (key_code_ == VKEY_RETURN))
854 return '\r'; 836 return '\r';
855 return GetCharacter(); 837 return GetCharacter();
856 } 838 }
857 839
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 else 880 else
899 set_flags(flags() & ~mask); 881 set_flags(flags() & ~mask);
900 } 882 }
901 883
902 KeyboardCode KeyEvent::GetLocatedWindowsKeyboardCode() const { 884 KeyboardCode KeyEvent::GetLocatedWindowsKeyboardCode() const {
903 return NonLocatedToLocatedKeyboardCode(key_code_, code_); 885 return NonLocatedToLocatedKeyboardCode(key_code_, code_);
904 } 886 }
905 887
906 uint16 KeyEvent::GetConflatedWindowsKeyCode() const { 888 uint16 KeyEvent::GetConflatedWindowsKeyCode() const {
907 if (is_char_) 889 if (is_char_)
908 return character_; 890 return key_;
909 return key_code_; 891 return key_code_;
910 } 892 }
911 893
912 std::string KeyEvent::GetCodeString() const { 894 std::string KeyEvent::GetCodeString() const {
913 return KeycodeConverter::DomCodeToCodeString(code_); 895 return KeycodeConverter::DomCodeToCodeString(code_);
914 } 896 }
915 897
916 //////////////////////////////////////////////////////////////////////////////// 898 ////////////////////////////////////////////////////////////////////////////////
917 // ScrollEvent 899 // ScrollEvent
918 900
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
973 gfx::PointF(x, y), 955 gfx::PointF(x, y),
974 time_stamp, 956 time_stamp,
975 flags | EF_FROM_TOUCH), 957 flags | EF_FROM_TOUCH),
976 details_(details) { 958 details_(details) {
977 } 959 }
978 960
979 GestureEvent::~GestureEvent() { 961 GestureEvent::~GestureEvent() {
980 } 962 }
981 963
982 } // namespace ui 964 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698