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 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"
33 #import "WindowsKeyboardCodes.h"
34 #import <Carbon/Carbon.h>
39 int windowsKeyCodeForKeyCode(uint16_t keyCode)
41 static const int windowsKeyCode[] = {
52 /* 0x0A */ VK_OEM_3, // "Section" - key to the left from 1 (ISO Keyboard Only)
66 /* 0x18 */ VK_OEM_PLUS, // =+
69 /* 0x1B */ VK_OEM_MINUS, // -_
72 /* 0x1E */ VK_OEM_6, // ]}
75 /* 0x21 */ VK_OEM_4, // {[
78 /* 0x24 */ VK_RETURN, // Return
81 /* 0x27 */ VK_OEM_7, // '"
83 /* 0x29 */ VK_OEM_1, // ;:
84 /* 0x2A */ VK_OEM_5, // \|
85 /* 0x2B */ VK_OEM_COMMA, // ,<
86 /* 0x2C */ VK_OEM_2, // /?
89 /* 0x2F */ VK_OEM_PERIOD, // .>
92 /* 0x32 */ VK_OEM_3, // `~
93 /* 0x33 */ VK_BACK, // Backspace
96 /* 0x36 */ VK_APPS, // Right Command
97 /* 0x37 */ VK_LWIN, // Left Command
98 /* 0x38 */ VK_LSHIFT, // Left Shift
99 /* 0x39 */ VK_CAPITAL, // Caps Lock
100 /* 0x3A */ VK_LMENU, // Left Option
101 /* 0x3B */ VK_LCONTROL, // Left Ctrl
102 /* 0x3C */ VK_RSHIFT, // Right Shift
103 /* 0x3D */ VK_RMENU, // Right Option
104 /* 0x3E */ VK_RCONTROL, // Right Ctrl
107 /* 0x41 */ VK_DECIMAL, // Num Pad .
109 /* 0x43 */ VK_MULTIPLY, // Num Pad *
111 /* 0x45 */ VK_ADD, // Num Pad +
113 /* 0x47 */ VK_CLEAR, // Num Pad Clear
114 /* 0x48 */ VK_VOLUME_UP,
115 /* 0x49 */ VK_VOLUME_DOWN,
116 /* 0x4A */ VK_VOLUME_MUTE,
117 /* 0x4B */ VK_DIVIDE, // Num Pad /
118 /* 0x4C */ VK_RETURN, // Num Pad Enter
120 /* 0x4E */ VK_SUBTRACT, // Num Pad -
123 /* 0x51 */ VK_OEM_PLUS, // Num Pad =. There is no such key on common PC keyboards, mapping to normal "+=".
124 /* 0x52 */ VK_NUMPAD0,
125 /* 0x53 */ VK_NUMPAD1,
126 /* 0x54 */ VK_NUMPAD2,
127 /* 0x55 */ VK_NUMPAD3,
128 /* 0x56 */ VK_NUMPAD4,
129 /* 0x57 */ VK_NUMPAD5,
130 /* 0x58 */ VK_NUMPAD6,
131 /* 0x59 */ VK_NUMPAD7,
133 /* 0x5B */ VK_NUMPAD8,
134 /* 0x5C */ VK_NUMPAD9,
135 /* 0x5D */ 0, // Yen (JIS Keyboard Only)
136 /* 0x5E */ 0, // Underscore (JIS Keyboard Only)
137 /* 0x5F */ 0, // KeypadComma (JIS Keyboard Only)
144 /* 0x66 */ 0, // Eisu (JIS Keyboard Only)
146 /* 0x68 */ 0, // Kana (JIS Keyboard Only)
152 /* 0x6E */ 0, // n/a (Windows95 key?)
156 /* 0x72 */ VK_INSERT, // Help
157 /* 0x73 */ VK_HOME, // Home
158 /* 0x74 */ VK_PRIOR, // Page Up
159 /* 0x75 */ VK_DELETE, // Forward Delete
161 /* 0x77 */ VK_END, // End
163 /* 0x79 */ VK_NEXT, // Page Down
165 /* 0x7B */ VK_LEFT, // Left Arrow
166 /* 0x7C */ VK_RIGHT, // Right Arrow
167 /* 0x7D */ VK_DOWN, // Down Arrow
168 /* 0x7E */ VK_UP, // Up Arrow
170 if (keyCode < WTF_ARRAY_LENGTH(windowsKeyCode))
171 return windowsKeyCode[keyCode];
175 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool backwardCompatibilityMode)
177 // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions.
178 ASSERT(m_type == KeyDown);
179 ASSERT(type == RawKeyDown || type == Char);
181 if (backwardCompatibilityMode)
184 if (type == RawKeyDown) {
186 m_unmodifiedText = String();
188 m_keyIdentifier = String();
189 m_windowsVirtualKeyCode = 0;
190 if (m_text.length() == 1 && (m_text[0U] >= 0xF700 && m_text[0U] <= 0xF7FF)) {
191 // According to NSEvents.h, OpenStep reserves the range 0xF700-0xF8FF for function keys. However, some actual private use characters
192 // happen to be in this range, e.g. the Apple logo (Option+Shift+K).
193 // 0xF7FF is an arbitrary cut-off.
195 m_unmodifiedText = String();
200 bool PlatformKeyboardEvent::currentCapsLockState()
202 return GetCurrentKeyModifiers() & alphaLock;
205 void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
207 UInt32 currentModifiers = GetCurrentKeyModifiers();
208 shiftKey = currentModifiers & ::shiftKey;
209 ctrlKey = currentModifiers & ::controlKey;
210 altKey = currentModifiers & ::optionKey;
211 metaKey = currentModifiers & ::cmdKey;
214 } // namespace WebCore
216 #endif // PLATFORM(MAC)