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 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 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"
32 #import "NotImplemented.h"
33 #import "WindowsKeyboardCodes.h"
34 #import <pal/spi/cocoa/IOKitSPI.h>
40 int windowsKeyCodeForKeyCode(uint16_t keyCode)
42 static const int windowsKeyCode[] = {
83 /* 0x28 */ VK_RETURN, // Return
85 /* 0x2A */ VK_BACK, // Backspace
88 /* 0x2D */ VK_OEM_MINUS, // -_
89 /* 0x2E */ VK_OEM_PLUS, // =+
90 /* 0x2F */ VK_OEM_4, // [{
91 /* 0x30 */ VK_OEM_6, // ]}
92 /* 0x31 */ VK_OEM_5, // \|
93 /* 0x32 */ 0, // kHIDUsage_KeyboardNonUSPound
94 /* 0x33 */ VK_OEM_1, // ;:
95 /* 0x34 */ VK_OEM_7, // '"
96 /* 0x35 */ VK_OEM_3, // `~
97 /* 0x36 */ VK_OEM_COMMA, // ,<
98 /* 0x37 */ VK_OEM_PERIOD, // .>
99 /* 0x38 */ VK_OEM_2, // /?
100 /* 0x39 */ VK_CAPITAL, // Caps Lock
113 /* 0x46 */ VK_SNAPSHOT, // Print Screen
114 /* 0x47 */ VK_SCROLL, // Scroll Lock
116 /* 0x49 */ VK_INSERT,
117 /* 0x4A */ VK_HOME, // Home
118 /* 0x4B */ VK_PRIOR, // Page Up
119 /* 0x4C */ VK_DELETE, // Forward Delete
120 /* 0x4D */ VK_END, // End
121 /* 0x4E */ VK_NEXT, // Page Down
122 /* 0x4F */ VK_RIGHT, // Right Arrow
123 /* 0x50 */ VK_LEFT, // Left Arrow
124 /* 0x51 */ VK_DOWN, // Down Arrow
125 /* 0x52 */ VK_UP, // Up Arrow
126 /* 0x53 */ VK_CLEAR, // Num Pad Clear
127 /* 0x54 */ VK_DIVIDE, // Num Pad /
128 /* 0x55 */ VK_MULTIPLY, // Num Pad *
129 /* 0x56 */ VK_SUBTRACT, // Num Pad -
130 /* 0x57 */ VK_ADD, // Num Pad +
131 /* 0x58 */ VK_RETURN, // Num Pad Enter
132 /* 0x59 */ VK_NUMPAD1,
133 /* 0x5A */ VK_NUMPAD2,
134 /* 0x5B */ VK_NUMPAD3,
135 /* 0x5C */ VK_NUMPAD4,
136 /* 0x5D */ VK_NUMPAD5,
137 /* 0x5E */ VK_NUMPAD6,
138 /* 0x5F */ VK_NUMPAD7,
139 /* 0x60 */ VK_NUMPAD8,
140 /* 0x61 */ VK_NUMPAD9,
141 /* 0x62 */ VK_NUMPAD0,
142 /* 0x63 */ VK_DECIMAL, // Num Pad .
143 /* 0x64 */ 0, // Non-ANSI \|
144 /* 0x65 */ 0, // Application
145 /* 0x66 */ 0, // Power
146 /* 0x67 */ VK_OEM_PLUS, // Num Pad =. There is no such key on common PC keyboards, mapping to normal "+=".
159 /* 0x74 */ VK_EXECUTE,
160 /* 0x75 */ VK_INSERT, // Help
161 /* 0x76 */ 0, // Menu
162 /* 0x77 */ VK_SELECT,
163 /* 0x78 */ 0, // Stop
164 /* 0x79 */ 0, // Again
165 /* 0x7A */ 0, // Undo
167 /* 0x7C */ 0, // Copy
168 /* 0x7D */ 0, // Paste
169 /* 0x7E */ 0, // Find
170 /* 0x7F */ VK_VOLUME_MUTE, // Mute
171 /* 0x80 */ VK_VOLUME_UP, // Volume Up
172 /* 0x81 */ VK_VOLUME_DOWN, // Volume Down
174 // Check if key is a modifier.
176 case kHIDUsage_KeyboardLeftControl:
178 case kHIDUsage_KeyboardLeftShift:
180 case kHIDUsage_KeyboardLeftAlt: // Left Option
182 case kHIDUsage_KeyboardLeftGUI: // Left Command
184 case kHIDUsage_KeyboardRightControl:
186 case kHIDUsage_KeyboardRightShift:
188 case kHIDUsage_KeyboardRightAlt: // Right Option
190 case kHIDUsage_KeyboardRightGUI: // Right Command
193 // Otherwise check all other known keys.
194 if (keyCode < WTF_ARRAY_LENGTH(windowsKeyCode))
195 return windowsKeyCode[keyCode];
196 return 0; // Unknown key
199 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool backwardCompatibilityMode)
201 // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions.
202 ASSERT(m_type == KeyDown);
203 ASSERT(type == RawKeyDown || type == Char);
205 if (backwardCompatibilityMode)
208 if (type == PlatformEvent::RawKeyDown) {
210 m_unmodifiedText = String();
212 m_keyIdentifier = String();
213 m_windowsVirtualKeyCode = 0;
214 if (m_text.length() == 1 && (m_text[0U] >= 0xF700 && m_text[0U] <= 0xF7FF)) {
215 // According to NSEvents.h, OpenStep reserves the range 0xF700-0xF8FF for function keys. However, some actual private use characters
216 // happen to be in this range, e.g. the Apple logo (Option+Shift+K).
217 // 0xF7FF is an arbitrary cut-off.
219 m_unmodifiedText = String();
224 bool PlatformKeyboardEvent::currentCapsLockState()
230 void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
241 #endif // PLATFORM(IOS)