[iOS] Key code is 0 for many hardware keyboard keys
[WebKit-https.git] / Source / WebCore / platform / ios / KeyEventIOS.mm
1 /*
2  * Copyright (C) 2004, 2006, 2007, 2008, 2009, 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(IOS)
30
31 #import "KeyEventCocoa.h"
32 #import "NotImplemented.h"
33 #import "WindowsKeyboardCodes.h"
34 #import <pal/spi/cocoa/IOKitSPI.h>
35
36 using namespace WTF;
37
38 namespace WebCore {
39
40 int windowsKeyCodeForKeyCode(uint16_t keyCode)
41 {
42     static const int windowsKeyCode[] = {
43         /* 0 */ 0, // n/a
44         /* 1 */ 0, // n/a
45         /* 2 */ 0, // n/a
46         /* 3 */ 0, // n/a
47         /* 4 */ VK_A,
48         /* 5 */ VK_B,
49         /* 6 */ VK_C,
50         /* 7 */ VK_D,
51         /* 8 */ VK_E,
52         /* 9 */ VK_F,
53         /* 0x0A */ VK_G,
54         /* 0x0B */ VK_H,
55         /* 0x0C */ VK_I,
56         /* 0x0D */ VK_J,
57         /* 0x0E */ VK_K,
58         /* 0x0F */ VK_L,
59         /* 0x10 */ VK_M,
60         /* 0x11 */ VK_N,
61         /* 0x12 */ VK_O,
62         /* 0x13 */ VK_P,
63         /* 0x14 */ VK_Q,
64         /* 0x15 */ VK_R,
65         /* 0x16 */ VK_S,
66         /* 0x17 */ VK_T,
67         /* 0x18 */ VK_U,
68         /* 0x19 */ VK_V,
69         /* 0x1A */ VK_W,
70         /* 0x1B */ VK_X,
71         /* 0x1C */ VK_Y,
72         /* 0x1D */ VK_Z,
73         /* 0x1E */ VK_1,
74         /* 0x1F */ VK_2,
75         /* 0x20 */ VK_3,
76         /* 0x21 */ VK_4,
77         /* 0x22 */ VK_5,
78         /* 0x23 */ VK_6,
79         /* 0x24 */ VK_7,
80         /* 0x25 */ VK_8,
81         /* 0x26 */ VK_9,
82         /* 0x27 */ VK_0,
83         /* 0x28 */ VK_RETURN, // Return
84         /* 0x29 */ VK_ESCAPE,
85         /* 0x2A */ VK_BACK, // Backspace
86         /* 0x2B */ VK_TAB,
87         /* 0x2C */ VK_SPACE,
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
101         /* 0x3A */ VK_F1,
102         /* 0x3B */ VK_F2,
103         /* 0x3C */ VK_F3,
104         /* 0x3D */ VK_F4,
105         /* 0x3E */ VK_F5,
106         /* 0x3F */ VK_F6,
107         /* 0x40 */ VK_F7,
108         /* 0x41 */ VK_F8,
109         /* 0x42 */ VK_F9,
110         /* 0x43 */ VK_F10,
111         /* 0x44 */ VK_F11,
112         /* 0x45 */ VK_F12,
113         /* 0x46 */ VK_SNAPSHOT, // Print Screen
114         /* 0x47 */ VK_SCROLL, // Scroll Lock
115         /* 0x48 */ VK_PAUSE,
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 "+=".
147         /* 0x68 */ VK_F13,
148         /* 0x69 */ VK_F14,
149         /* 0x6A */ VK_F15,
150         /* 0x6B */ VK_F16,
151         /* 0x6C */ VK_F17,
152         /* 0x6D */ VK_F18,
153         /* 0x6E */ VK_F19,
154         /* 0x6F */ VK_F20,
155         /* 0x70 */ VK_F21,
156         /* 0x71 */ VK_F22,
157         /* 0x72 */ VK_F23,
158         /* 0x73 */ VK_F24,
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
166         /* 0x7B */ 0, // Cut
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
173     };
174     // Check if key is a modifier.
175     switch (keyCode) {
176     case kHIDUsage_KeyboardLeftControl:
177         return VK_LCONTROL;
178     case kHIDUsage_KeyboardLeftShift:
179         return VK_LSHIFT;
180     case kHIDUsage_KeyboardLeftAlt: // Left Option
181         return VK_LMENU;
182     case kHIDUsage_KeyboardLeftGUI: // Left Command
183         return VK_LWIN;
184     case kHIDUsage_KeyboardRightControl:
185         return VK_RCONTROL;
186     case kHIDUsage_KeyboardRightShift:
187         return VK_RSHIFT;
188     case kHIDUsage_KeyboardRightAlt: // Right Option
189         return VK_RMENU;
190     case kHIDUsage_KeyboardRightGUI: // Right Command
191         return VK_APPS;
192     }
193     // Otherwise check all other known keys.
194     if (keyCode < WTF_ARRAY_LENGTH(windowsKeyCode))
195         return windowsKeyCode[keyCode];
196     return 0; // Unknown key
197 }
198
199 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool backwardCompatibilityMode)
200 {
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);
204     m_type = type;
205     if (backwardCompatibilityMode)
206         return;
207
208     if (type == PlatformEvent::RawKeyDown) {
209         m_text = String();
210         m_unmodifiedText = String();
211     } else {
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.
218             m_text = String();
219             m_unmodifiedText = String();
220         }
221     }
222 }
223
224 bool PlatformKeyboardEvent::currentCapsLockState()
225 {
226     notImplemented();
227     return false;
228 }
229
230 void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey)
231 {
232     notImplemented();
233     shiftKey = false;
234     ctrlKey = false;
235     altKey = false;
236     metaKey = false;
237 }
238
239 }
240
241 #endif // PLATFORM(IOS)