2 * Copyright (C) 2004, 2006, 2007, 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"
33 #import "WindowsKeyboardCodes.h"
34 #import <Carbon/Carbon.h>
40 static bool isKeypadEvent(NSEvent* event)
42 // Check that this is the type of event that has a keyCode.
43 switch ([event type]) {
52 if ([event modifierFlags] & NSNumericPadKeyMask)
55 switch ([event keyCode]) {
80 static inline bool isKeyUpEvent(NSEvent *event)
82 if ([event type] != NSFlagsChanged)
83 return [event type] == NSKeyUp;
84 // FIXME: This logic fails if the user presses both Shift keys at once, for example:
85 // we treat releasing one of them as keyDown.
86 switch ([event keyCode]) {
87 case 54: // Right Command
88 case 55: // Left Command
89 return ([event modifierFlags] & NSCommandKeyMask) == 0;
92 return ([event modifierFlags] & NSAlphaShiftKeyMask) == 0;
94 case 56: // Left Shift
95 case 60: // Right Shift
96 return ([event modifierFlags] & NSShiftKeyMask) == 0;
100 return ([event modifierFlags] & NSAlternateKeyMask) == 0;
102 case 59: // Left Ctrl
103 case 62: // Right Ctrl
104 return ([event modifierFlags] & NSControlKeyMask) == 0;
107 return ([event modifierFlags] & NSFunctionKeyMask) == 0;
112 static inline String textFromEvent(NSEvent* event)
114 if ([event type] == NSFlagsChanged)
116 return [event characters];
120 static inline String unmodifiedTextFromEvent(NSEvent* event)
122 if ([event type] == NSFlagsChanged)
124 return [event charactersIgnoringModifiers];
127 static String keyIdentifierForKeyEvent(NSEvent* event)
129 if ([event type] == NSFlagsChanged)
130 switch ([event keyCode]) {
131 case 54: // Right Command
132 case 55: // Left Command
138 case 56: // Left Shift
139 case 60: // Right Shift
143 case 61: // Right Alt
146 case 59: // Left Ctrl
147 case 62: // Right Ctrl
151 ASSERT_NOT_REACHED();
155 NSString *s = [event charactersIgnoringModifiers];
156 if ([s length] != 1) {
157 LOG(Events, "received an unexpected number of characters in key event: %u", [s length]);
158 return "Unidentified";
160 return keyIdentifierForCharCode([s characterAtIndex:0]);
163 static int windowsKeyCodeForKeyEvent(NSEvent *event)
166 // There are several kinds of characters for which we produce key code from char code:
167 // 1. Roman letters. Windows keyboard layouts affect both virtual key codes and character codes for these,
168 // so e.g. 'A' gets the same keyCode on QWERTY, AZERTY or Dvorak layouts.
169 // 2. Keys for which there is no known Mac virtual key codes, like PrintScreen.
170 // 3. Certain punctuation keys. On Windows, these are also remapped depending on current keyboard layout,
171 // but see comment in windowsKeyCodeForCharCode().
172 if ([event type] == NSKeyDown || [event type] == NSKeyUp) {
173 // Cmd switches Roman letters for Dvorak-QWERTY layout, so try modified characters first.
174 NSString* s = [event characters];
175 code = [s length] > 0 ? windowsKeyCodeForCharCode([s characterAtIndex:0]) : 0;
179 // Ctrl+A on an AZERTY keyboard would get VK_Q keyCode if we relied on -[NSEvent keyCode] below.
180 s = [event charactersIgnoringModifiers];
181 code = [s length] > 0 ? windowsKeyCodeForCharCode([s characterAtIndex:0]) : 0;
186 // Map Mac virtual key code directly to Windows one for any keys not handled above.
187 // E.g. the key next to Caps Lock has the same Event.keyCode on U.S. keyboard ('A') and on Russian keyboard (CYRILLIC LETTER EF).
188 return windowsKeyCodeForKeyCode([event keyCode]);
191 PlatformKeyboardEvent::PlatformKeyboardEvent(NSEvent *event)
192 : m_type(isKeyUpEvent(event) ? PlatformKeyboardEvent::KeyUp : PlatformKeyboardEvent::KeyDown)
193 , m_text(textFromEvent(event))
194 , m_unmodifiedText(unmodifiedTextFromEvent(event))
195 , m_keyIdentifier(keyIdentifierForKeyEvent(event))
196 , m_autoRepeat(([event type] != NSFlagsChanged) && [event isARepeat])
197 , m_windowsVirtualKeyCode(windowsKeyCodeForKeyEvent(event))
198 , m_nativeVirtualKeyCode([event keyCode])
199 , m_isKeypad(isKeypadEvent(event))
200 , m_shiftKey([event modifierFlags] & NSShiftKeyMask)
201 , m_ctrlKey([event modifierFlags] & NSControlKeyMask)
202 , m_altKey([event modifierFlags] & NSAlternateKeyMask)
203 , m_metaKey([event modifierFlags] & NSCommandKeyMask)
206 // Always use 13 for Enter/Return -- we don't want to use AppKit's different character for Enter.
207 if (m_windowsVirtualKeyCode == VK_RETURN) {
209 m_unmodifiedText = "\r";
212 // AppKit sets text to "\x7F" for backspace, but the correct KeyboardEvent character code is 8.
213 if (m_windowsVirtualKeyCode == VK_BACK) {
215 m_unmodifiedText = "\x8";
218 // Always use 9 for Tab -- we don't want to use AppKit's different character for shift-tab.
219 if (m_windowsVirtualKeyCode == VK_TAB) {
221 m_unmodifiedText = "\x9";
225 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool backwardCompatibilityMode)
227 // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions.
228 ASSERT(m_type == KeyDown);
229 ASSERT(type == RawKeyDown || type == Char);
231 if (backwardCompatibilityMode)
234 if (type == RawKeyDown) {
236 m_unmodifiedText = String();
238 m_keyIdentifier = String();
239 m_windowsVirtualKeyCode = 0;
240 if (m_text.length() == 1 && (m_text[0U] >= 0xF700 && m_text[0U] <= 0xF7FF)) {
241 // According to NSEvents.h, OpenStep reserves the range 0xF700-0xF8FF for function keys. However, some actual private use characters
242 // happen to be in this range, e.g. the Apple logo (Option+Shift+K).
243 // 0xF7FF is an arbitrary cut-off.
245 m_unmodifiedText = String();
250 bool PlatformKeyboardEvent::currentCapsLockState()
252 return GetCurrentKeyModifiers() & alphaLock;
255 void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
257 UInt32 currentModifiers = GetCurrentKeyModifiers();
258 shiftKey = currentModifiers & ::shiftKey;
259 ctrlKey = currentModifiers & ::controlKey;
260 altKey = currentModifiers & ::optionKey;
261 metaKey = currentModifiers & ::cmdKey;
266 #endif // PLATFORM(MAC)