OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #include "config.h" | 7 #include "config.h" |
8 | 8 |
9 #include "wtf/ASCIICType.h" | 9 #include "wtf/ASCIICType.h" |
10 #include "webkit/glue/webinputevent.h" | 10 #include "webkit/glue/webinputevent.h" |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 134 |
135 // ---------------------------------------------------------------------- | 135 // ---------------------------------------------------------------------- |
136 // Begin Apple code, copied from KeyEventMac.mm | 136 // Begin Apple code, copied from KeyEventMac.mm |
137 // | 137 // |
138 // We can share some of this code if we factored it out of KeyEventMac, but | 138 // We can share some of this code if we factored it out of KeyEventMac, but |
139 // the main problem is that it relies on the NSString ctor on String for | 139 // the main problem is that it relies on the NSString ctor on String for |
140 // conversions, and since we're building without PLATFORM(MAC), we don't have | 140 // conversions, and since we're building without PLATFORM(MAC), we don't have |
141 // that. As a result we have to use NSString here exclusively and thus tweak | 141 // that. As a result we have to use NSString here exclusively and thus tweak |
142 // the code so it's not re-usable as-is. One possiblity would be to make the | 142 // the code so it's not re-usable as-is. One possiblity would be to make the |
143 // upstream code only use NSString, but I'm not certain how far that change | 143 // upstream code only use NSString, but I'm not certain how far that change |
144 // would propagate. | 144 // would propageage |
145 | 145 |
146 namespace WebCore { | 146 namespace WebCore { |
147 | 147 |
148 static inline bool isKeyUpEvent(NSEvent *event) | 148 static inline bool isKeyUpEvent(NSEvent *event) |
149 { | 149 { |
150 if ([event type] != NSFlagsChanged) | 150 if ([event type] != NSFlagsChanged) |
151 return [event type] == NSKeyUp; | 151 return [event type] == NSKeyUp; |
152 // FIXME: This logic fails if the user presses both Shift keys at once, for
example: | 152 // FIXME: This logic fails if the user presses both Shift keys at once, for
example: |
153 // we treat releasing one of them as keyDown. | 153 // we treat releasing one of them as keyDown. |
154 switch ([event keyCode]) { | 154 switch ([event keyCode]) { |
(...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
916 default: | 916 default: |
917 return [NSString stringWithFormat:@"U+%04X", WTF::toASCIIUpper(c)]; | 917 return [NSString stringWithFormat:@"U+%04X", WTF::toASCIIUpper(c)]; |
918 } | 918 } |
919 } | 919 } |
920 | 920 |
921 } // namespace WebCore | 921 } // namespace WebCore |
922 | 922 |
923 // End Apple code. | 923 // End Apple code. |
924 // --------------------------------------------------------------------- | 924 // --------------------------------------------------------------------- |
925 | 925 |
| 926 // Helper that copies the unichar characters of a NSString into a suitably |
| 927 // resized vector. The vector will be null terminated and thus even if the |
| 928 // string is empty or nil, it array will have a NUL. |
| 929 static void FillVectorFromNSString(std::vector<unsigned short>* v, |
| 930 NSString* str) { |
| 931 const unsigned int length = [str length]; |
| 932 v->reserve(length + 1); |
| 933 [str getCharacters:&(*v)[0]]; |
| 934 (*v)[length] = '\0'; |
| 935 } |
| 936 |
926 WebKeyboardEvent::WebKeyboardEvent(NSEvent *event) { | 937 WebKeyboardEvent::WebKeyboardEvent(NSEvent *event) { |
927 system_key = false; | |
928 type = WebCore::isKeyUpEvent(event) ? KEY_UP : KEY_DOWN; | 938 type = WebCore::isKeyUpEvent(event) ? KEY_UP : KEY_DOWN; |
929 | 939 |
930 if ([event modifierFlags] & NSControlKeyMask) | 940 if ([event modifierFlags] & NSControlKeyMask) |
931 modifiers |= CTRL_KEY; | 941 modifiers |= CTRL_KEY; |
932 if ([event modifierFlags] & NSShiftKeyMask) | 942 if ([event modifierFlags] & NSShiftKeyMask) |
933 modifiers |= SHIFT_KEY; | 943 modifiers |= SHIFT_KEY; |
934 if ([event modifierFlags] & NSAlternateKeyMask) | 944 if ([event modifierFlags] & NSAlternateKeyMask) |
935 modifiers |= ALT_KEY; | 945 modifiers |= ALT_KEY; |
936 if ([event modifierFlags] & NSCommandKeyMask) | 946 if ([event modifierFlags] & NSCommandKeyMask) |
937 modifiers |= META_KEY; | 947 modifiers |= META_KEY; |
938 | 948 |
939 if (WebCore::isKeypadEvent(event)) | 949 if (WebCore::isKeypadEvent(event)) |
940 modifiers |= IS_KEYPAD; | 950 modifiers |= IS_KEYPAD; |
941 | 951 |
942 if (([event type] != NSFlagsChanged) && [event isARepeat]) | 952 if (([event type] != NSFlagsChanged) && [event isARepeat]) |
943 modifiers |= IS_AUTO_REPEAT; | 953 modifiers |= IS_AUTO_REPEAT; |
944 | 954 |
945 windows_key_code = WebCore::windowsKeyCodeForKeyEvent(event); | |
946 native_key_code = [event keyCode]; | |
947 | |
948 NSString* textString = WebCore::textFromEvent(event); | 955 NSString* textString = WebCore::textFromEvent(event); |
949 NSString* unmodifiedStr = WebCore::unmodifiedTextFromEvent(event); | 956 NSString* unmodifiedStr = WebCore::unmodifiedTextFromEvent(event); |
950 NSString* identStr = WebCore::keyIdentifierForKeyEvent(event); | 957 NSString* identStr = WebCore::keyIdentifierForKeyEvent(event); |
951 | 958 FillVectorFromNSString(&text, textString); |
952 // Begin Apple code, copied from KeyEventMac.mm | 959 FillVectorFromNSString(&unmodified_text, unmodifiedStr); |
953 | 960 FillVectorFromNSString(&key_identifier, identStr); |
954 // Always use 13 for Enter/Return -- we don't want to use AppKit's | 961 |
955 // different character for Enter. | 962 key_code = WebCore::windowsKeyCodeForKeyEvent(event); |
956 if (windows_key_code == '\r') { | |
957 textString = @"\r"; | |
958 unmodifiedStr = @"\r"; | |
959 } | |
960 | |
961 // The adjustments below are only needed in backward compatibility mode, | |
962 // but we cannot tell what mode we are in from here. | |
963 | |
964 // Turn 0x7F into 8, because backspace needs to always be 8. | |
965 if ([textString isEqualToString:@"\x7F"]) | |
966 textString = @"\x8"; | |
967 if ([unmodifiedStr isEqualToString:@"\x7F"]) | |
968 unmodifiedStr = @"\x8"; | |
969 // Always use 9 for tab -- we don't want to use AppKit's different character f
or shift-tab. | |
970 if (windows_key_code == 9) { | |
971 textString = @"\x9"; | |
972 unmodifiedStr = @"\x9"; | |
973 } | |
974 | |
975 // End Apple code. | |
976 | |
977 memset(&text, 0, sizeof(text)); | |
978 memset(&unmodified_text, 0, sizeof(unmodified_text)); | |
979 memset(&key_identifier, 0, sizeof(key_identifier)); | |
980 | |
981 if ([textString length] < kTextLengthCap && | |
982 [unmodifiedStr length] < kTextLengthCap) { | |
983 [textString getCharacters:&text[0]]; | |
984 [unmodifiedStr getCharacters:&unmodified_text[0]]; | |
985 } else { | |
986 LOG(ERROR) << "Event had text too long; dropped"; | |
987 } | |
988 [identStr getCString:&key_identifier[0] | |
989 maxLength:kIdentifierLengthCap | |
990 encoding:NSASCIIStringEncoding]; | |
991 } | 963 } |
OLD | NEW |