2 * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #import "PlatformKeyboardEvent.h"
31 #import "KeyEventCocoa.h"
39 static String keyIdentifierForKeyEvent(WebEvent *event)
41 NSString *s = event.charactersIgnoringModifiers;
42 if ([s length] != 1) {
43 LOG(Events, "received an unexpected number of characters in key event: %u", [s length]);
44 return "Unidentified";
47 unichar c = CFStringGetCharacterAtIndex((CFStringRef)s, 0);
48 return keyIdentifierForCharCode(c);
51 PlatformKeyboardEvent::PlatformKeyboardEvent(WebEvent *event)
52 : m_type(event.type == WebEventKeyUp ? PlatformKeyboardEvent::KeyUp : PlatformKeyboardEvent::KeyDown)
53 , m_text(event.characters)
54 , m_unmodifiedText(event.charactersIgnoringModifiers)
55 , m_keyIdentifier(keyIdentifierForKeyEvent(event))
56 , m_autoRepeat(event.isKeyRepeating)
57 , m_windowsVirtualKeyCode(event.keyCode)
58 , m_isKeypad(false) // iPhone does not distinguish the numpad <rdar://problem/7190835>
59 , m_shiftKey(event.modifierFlags & WebEventFlagMaskShift)
60 , m_ctrlKey(event.modifierFlags & WebEventFlagMaskControl)
61 , m_altKey(event.modifierFlags & WebEventFlagMaskAlternate)
62 , m_metaKey(event.modifierFlags & WebEventFlagMaskCommand)
65 ASSERT(event.type == WebEventKeyDown || event.type == WebEventKeyUp);
67 // Always use 13 for Enter/Return -- we don't want to use AppKit's different character for Enter.
68 if (m_windowsVirtualKeyCode == '\r') {
70 m_unmodifiedText = "\r";
73 // The adjustments below are only needed in backward compatibility mode, but we cannot tell what mode we are in from here.
75 // Turn 0x7F into 8, because backspace needs to always be 8.
78 if (m_unmodifiedText == "\x7F")
79 m_unmodifiedText = "\x8";
80 // Always use 9 for tab -- we don't want to use AppKit's different character for shift-tab.
81 if (m_windowsVirtualKeyCode == 9) {
83 m_unmodifiedText = "\x9";
87 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool backwardCompatibilityMode)
89 // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions.
90 ASSERT(m_type == KeyDown);
91 ASSERT(type == RawKeyDown || type == Char);
93 if (backwardCompatibilityMode)
96 if (type == RawKeyDown) {
98 m_unmodifiedText = String();
100 m_keyIdentifier = String();
101 m_windowsVirtualKeyCode = 0;
102 if (m_text.length() == 1 && (m_text[0U] >= 0xF700 && m_text[0U] <= 0xF7FF)) {
103 // According to NSEvents.h, OpenStep reserves the range 0xF700-0xF8FF for function keys. However, some actual private use characters
104 // happen to be in this range, e.g. the Apple logo (Option+Shift+K).
105 // 0xF7FF is an arbitrary cut-off.
107 m_unmodifiedText = String();
112 bool PlatformKeyboardEvent::currentCapsLockState()
119 #endif // PLATFORM(IOS)