[iOS] Key code is 0 for many hardware keyboard keys
[WebKit-https.git] / Source / WebCore / platform / mac / KeyEventMac.mm
1 /*
2  * Copyright (C) 2004, 2006, 2007, 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #import "config.h"
27 #import "PlatformKeyboardEvent.h"
28
29 #if PLATFORM(MAC)
30
31 #import "KeyEventCocoa.h"
32 #import "Logging.h"
33 #import "WindowsKeyboardCodes.h"
34 #import <Carbon/Carbon.h>
35
36 namespace WebCore {
37 using namespace WTF;
38
39 int windowsKeyCodeForKeyCode(uint16_t keyCode)
40 {
41     static const int windowsKeyCode[] = {
42         /* 0 */ VK_A,
43         /* 1 */ VK_S,
44         /* 2 */ VK_D,
45         /* 3 */ VK_F,
46         /* 4 */ VK_H,
47         /* 5 */ VK_G,
48         /* 6 */ VK_Z,
49         /* 7 */ VK_X,
50         /* 8 */ VK_C,
51         /* 9 */ VK_V,
52         /* 0x0A */ VK_OEM_3, // "Section" - key to the left from 1 (ISO Keyboard Only)
53         /* 0x0B */ VK_B,
54         /* 0x0C */ VK_Q,
55         /* 0x0D */ VK_W,
56         /* 0x0E */ VK_E,
57         /* 0x0F */ VK_R,
58         /* 0x10 */ VK_Y,
59         /* 0x11 */ VK_T,
60         /* 0x12 */ VK_1,
61         /* 0x13 */ VK_2,
62         /* 0x14 */ VK_3,
63         /* 0x15 */ VK_4,
64         /* 0x16 */ VK_6,
65         /* 0x17 */ VK_5,
66         /* 0x18 */ VK_OEM_PLUS, // =+
67         /* 0x19 */ VK_9,
68         /* 0x1A */ VK_7,
69         /* 0x1B */ VK_OEM_MINUS, // -_
70         /* 0x1C */ VK_8,
71         /* 0x1D */ VK_0,
72         /* 0x1E */ VK_OEM_6, // ]}
73         /* 0x1F */ VK_O,
74         /* 0x20 */ VK_U,
75         /* 0x21 */ VK_OEM_4, // {[
76         /* 0x22 */ VK_I,
77         /* 0x23 */ VK_P,
78         /* 0x24 */ VK_RETURN, // Return
79         /* 0x25 */ VK_L,
80         /* 0x26 */ VK_J,
81         /* 0x27 */ VK_OEM_7, // '"
82         /* 0x28 */ VK_K,
83         /* 0x29 */ VK_OEM_1, // ;:
84         /* 0x2A */ VK_OEM_5, // \|
85         /* 0x2B */ VK_OEM_COMMA, // ,<
86         /* 0x2C */ VK_OEM_2, // /?
87         /* 0x2D */ VK_N,
88         /* 0x2E */ VK_M,
89         /* 0x2F */ VK_OEM_PERIOD, // .>
90         /* 0x30 */ VK_TAB,
91         /* 0x31 */ VK_SPACE,
92         /* 0x32 */ VK_OEM_3, // `~
93         /* 0x33 */ VK_BACK, // Backspace
94         /* 0x34 */ 0, // n/a
95         /* 0x35 */ VK_ESCAPE,
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
105         /* 0x3F */ 0, // fn
106         /* 0x40 */ VK_F17,
107         /* 0x41 */ VK_DECIMAL, // Num Pad .
108         /* 0x42 */ 0, // n/a
109         /* 0x43 */ VK_MULTIPLY, // Num Pad *
110         /* 0x44 */ 0, // n/a
111         /* 0x45 */ VK_ADD, // Num Pad +
112         /* 0x46 */ 0, // n/a
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
119         /* 0x4D */ 0, // n/a
120         /* 0x4E */ VK_SUBTRACT, // Num Pad -
121         /* 0x4F */ VK_F18,
122         /* 0x50 */ VK_F19,
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,
132         /* 0x5A */ VK_F20,
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)
138         /* 0x60 */ VK_F5,
139         /* 0x61 */ VK_F6,
140         /* 0x62 */ VK_F7,
141         /* 0x63 */ VK_F3,
142         /* 0x64 */ VK_F8,
143         /* 0x65 */ VK_F9,
144         /* 0x66 */ 0, // Eisu (JIS Keyboard Only)
145         /* 0x67 */ VK_F11,
146         /* 0x68 */ 0, // Kana (JIS Keyboard Only)
147         /* 0x69 */ VK_F13,
148         /* 0x6A */ VK_F16,
149         /* 0x6B */ VK_F14,
150         /* 0x6C */ 0, // n/a
151         /* 0x6D */ VK_F10,
152         /* 0x6E */ 0, // n/a (Windows95 key?)
153         /* 0x6F */ VK_F12,
154         /* 0x70 */ 0, // n/a
155         /* 0x71 */ VK_F15,
156         /* 0x72 */ VK_INSERT, // Help
157         /* 0x73 */ VK_HOME, // Home
158         /* 0x74 */ VK_PRIOR, // Page Up
159         /* 0x75 */ VK_DELETE, // Forward Delete
160         /* 0x76 */ VK_F4,
161         /* 0x77 */ VK_END, // End
162         /* 0x78 */ VK_F2,
163         /* 0x79 */ VK_NEXT, // Page Down
164         /* 0x7A */ VK_F1,
165         /* 0x7B */ VK_LEFT, // Left Arrow
166         /* 0x7C */ VK_RIGHT, // Right Arrow
167         /* 0x7D */ VK_DOWN, // Down Arrow
168         /* 0x7E */ VK_UP, // Up Arrow
169     };
170     if (keyCode < WTF_ARRAY_LENGTH(windowsKeyCode))
171         return windowsKeyCode[keyCode];
172     return 0;
173 }
174
175 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool backwardCompatibilityMode)
176 {
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);
180     m_type = type;
181     if (backwardCompatibilityMode)
182         return;
183
184     if (type == RawKeyDown) {
185         m_text = String();
186         m_unmodifiedText = String();
187     } else {
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.
194             m_text = String();
195             m_unmodifiedText = String();
196         }
197     }
198 }
199
200 bool PlatformKeyboardEvent::currentCapsLockState()
201 {
202     return GetCurrentKeyModifiers() & alphaLock;
203 }
204
205 void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
206 {
207     UInt32 currentModifiers = GetCurrentKeyModifiers();
208     shiftKey = currentModifiers & ::shiftKey;
209     ctrlKey = currentModifiers & ::controlKey;
210     altKey = currentModifiers & ::optionKey;
211     metaKey = currentModifiers & ::cmdKey;
212 }
213
214 } // namespace WebCore
215
216 #endif // PLATFORM(MAC)