OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 /* |
2 // Use of this source code is governed by a BSD-style license that can be | 2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. |
3 // found in the LICENSE file. | 3 * Copyright (C) 2006-2009 Google Inc. |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions |
| 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright |
| 11 * notice, this list of conditions and the following disclaimer in the |
| 12 * documentation and/or other materials provided with the distribution. |
| 13 * |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 */ |
4 | 26 |
5 #import <Cocoa/Cocoa.h> | 27 #import <Cocoa/Cocoa.h> |
6 | 28 |
7 #include "config.h" | 29 #include "config.h" |
8 | 30 |
9 #include "wtf/ASCIICType.h" | 31 #include "wtf/ASCIICType.h" |
10 #include "webkit/glue/webinputevent.h" | 32 #include "webkit/glue/webinputevent.h" |
11 #include "webkit/glue/event_conversion.h" | 33 #include "webkit/glue/event_conversion.h" |
12 | 34 |
13 #undef LOG | 35 #undef LOG |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 156 |
135 // ---------------------------------------------------------------------- | 157 // ---------------------------------------------------------------------- |
136 // Begin Apple code, copied from KeyEventMac.mm | 158 // Begin Apple code, copied from KeyEventMac.mm |
137 // | 159 // |
138 // We can share some of this code if we factored it out of KeyEventMac, but | 160 // 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 | 161 // 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 | 162 // 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 | 163 // 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 | 164 // 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 | 165 // upstream code only use NSString, but I'm not certain how far that change |
144 // would propageage | 166 // would propagate. |
145 | 167 |
146 namespace WebCore { | 168 namespace WebCore { |
147 | 169 |
148 static inline bool isKeyUpEvent(NSEvent *event) | 170 static inline bool isKeyUpEvent(NSEvent *event) |
149 { | 171 { |
150 if ([event type] != NSFlagsChanged) | 172 if ([event type] != NSFlagsChanged) |
151 return [event type] == NSKeyUp; | 173 return [event type] == NSKeyUp; |
152 // FIXME: This logic fails if the user presses both Shift keys at once, for
example: | 174 // FIXME: This logic fails if the user presses both Shift keys at once, for
example: |
153 // we treat releasing one of them as keyDown. | 175 // we treat releasing one of them as keyDown. |
154 switch ([event keyCode]) { | 176 switch ([event keyCode]) { |
(...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
916 default: | 938 default: |
917 return [NSString stringWithFormat:@"U+%04X", WTF::toASCIIUpper(c)]; | 939 return [NSString stringWithFormat:@"U+%04X", WTF::toASCIIUpper(c)]; |
918 } | 940 } |
919 } | 941 } |
920 | 942 |
921 } // namespace WebCore | 943 } // namespace WebCore |
922 | 944 |
923 // End Apple code. | 945 // End Apple code. |
924 // --------------------------------------------------------------------- | 946 // --------------------------------------------------------------------- |
925 | 947 |
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 | |
937 WebKeyboardEvent::WebKeyboardEvent(NSEvent *event) { | 948 WebKeyboardEvent::WebKeyboardEvent(NSEvent *event) { |
| 949 system_key = false; |
938 type = WebCore::isKeyUpEvent(event) ? KEY_UP : KEY_DOWN; | 950 type = WebCore::isKeyUpEvent(event) ? KEY_UP : KEY_DOWN; |
939 | 951 |
940 if ([event modifierFlags] & NSControlKeyMask) | 952 if ([event modifierFlags] & NSControlKeyMask) |
941 modifiers |= CTRL_KEY; | 953 modifiers |= CTRL_KEY; |
942 if ([event modifierFlags] & NSShiftKeyMask) | 954 if ([event modifierFlags] & NSShiftKeyMask) |
943 modifiers |= SHIFT_KEY; | 955 modifiers |= SHIFT_KEY; |
944 if ([event modifierFlags] & NSAlternateKeyMask) | 956 if ([event modifierFlags] & NSAlternateKeyMask) |
945 modifiers |= ALT_KEY; | 957 modifiers |= ALT_KEY; |
946 if ([event modifierFlags] & NSCommandKeyMask) | 958 if ([event modifierFlags] & NSCommandKeyMask) |
947 modifiers |= META_KEY; | 959 modifiers |= META_KEY; |
948 | 960 |
949 if (WebCore::isKeypadEvent(event)) | 961 if (WebCore::isKeypadEvent(event)) |
950 modifiers |= IS_KEYPAD; | 962 modifiers |= IS_KEYPAD; |
951 | 963 |
952 if (([event type] != NSFlagsChanged) && [event isARepeat]) | 964 if (([event type] != NSFlagsChanged) && [event isARepeat]) |
953 modifiers |= IS_AUTO_REPEAT; | 965 modifiers |= IS_AUTO_REPEAT; |
954 | 966 |
955 NSString* textString = WebCore::textFromEvent(event); | 967 windows_key_code = WebCore::windowsKeyCodeForKeyEvent(event); |
956 NSString* unmodifiedStr = WebCore::unmodifiedTextFromEvent(event); | 968 native_key_code = [event keyCode]; |
957 NSString* identStr = WebCore::keyIdentifierForKeyEvent(event); | |
958 FillVectorFromNSString(&text, textString); | |
959 FillVectorFromNSString(&unmodified_text, unmodifiedStr); | |
960 FillVectorFromNSString(&key_identifier, identStr); | |
961 | 969 |
962 key_code = WebCore::windowsKeyCodeForKeyEvent(event); | 970 NSString* text_str = WebCore::textFromEvent(event); |
| 971 NSString* unmodified_str = WebCore::unmodifiedTextFromEvent(event); |
| 972 NSString* identifier_str = WebCore::keyIdentifierForKeyEvent(event); |
| 973 |
| 974 // Begin Apple code, copied from KeyEventMac.mm |
| 975 |
| 976 // Always use 13 for Enter/Return -- we don't want to use AppKit's |
| 977 // different character for Enter. |
| 978 if (windows_key_code == '\r') { |
| 979 text_str = @"\r"; |
| 980 unmodified_str = @"\r"; |
| 981 } |
| 982 |
| 983 // The adjustments below are only needed in backward compatibility mode, |
| 984 // but we cannot tell what mode we are in from here. |
| 985 |
| 986 // Turn 0x7F into 8, because backspace needs to always be 8. |
| 987 if ([text_str isEqualToString:@"\x7F"]) |
| 988 text_str = @"\x8"; |
| 989 if ([unmodified_str isEqualToString:@"\x7F"]) |
| 990 unmodified_str = @"\x8"; |
| 991 // Always use 9 for tab -- we don't want to use AppKit's different character |
| 992 // for shift-tab. |
| 993 if (windows_key_code == 9) { |
| 994 text_str = @"\x9"; |
| 995 unmodified_str = @"\x9"; |
| 996 } |
| 997 |
| 998 // End Apple code. |
| 999 |
| 1000 memset(&text, 0, sizeof(text)); |
| 1001 memset(&unmodified_text, 0, sizeof(unmodified_text)); |
| 1002 memset(&key_identifier, 0, sizeof(key_identifier)); |
| 1003 |
| 1004 if ([text_str length] < kTextLengthCap && |
| 1005 [unmodified_str length] < kTextLengthCap) { |
| 1006 [text_str getCharacters:&text[0]]; |
| 1007 [unmodified_str getCharacters:&unmodified_text[0]]; |
| 1008 } else { |
| 1009 LOG(ERROR) << "Event had text too long; dropped"; |
| 1010 } |
| 1011 [identifier_str getCString:&key_identifier[0] |
| 1012 maxLength:kIdentifierLengthCap |
| 1013 encoding:NSASCIIStringEncoding]; |
963 } | 1014 } |
OLD | NEW |