1 /* 2 * Copyright (C) 2007 Ryan Leavengood <leavengood (at) gmail.com> 3 * Copyright (C) 2008 Andrea Anzani <andrea.anzani (at) gmail.com> 4 * Copyright (C) 2010 Stephan Amus <superstippi (at) gmx.de> 5 * 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include "config.h" 31 #include "PlatformKeyboardEvent.h" 32 33 #include "NotImplemented.h" 34 #include "WindowsKeyboardCodes.h" 35 #include <InterfaceDefs.h> 36 #include <Message.h> 37 #include <String.h> 38 #include <wtf/text/CString.h> 39 40 41 namespace WebCore { 42 43 static String keyIdentifierForHaikuKeyCode(char singleByte, int keyCode) 44 { 45 switch (singleByte) { 46 case B_FUNCTION_KEY: 47 48 switch (keyCode) { 49 case B_F1_KEY: 50 return "F1"; 51 case B_F2_KEY: 52 return "F2"; 53 case B_F3_KEY: 54 return "F3"; 55 case B_F4_KEY: 56 return "F4"; 57 case B_F5_KEY: 58 return "F5"; 59 case B_F6_KEY: 60 return "F6"; 61 case B_F7_KEY: 62 return "F7"; 63 case B_F8_KEY: 64 return "F8"; 65 case B_F9_KEY: 66 return "F9"; 67 case B_F10_KEY: 68 return "F10"; 69 case B_F11_KEY: 70 return "F11"; 71 case B_F12_KEY: 72 return "F12"; 73 case B_PRINT_KEY: 74 return "Print"; 75 case B_PAUSE_KEY: 76 return "Pause"; 77 case B_SCROLL_KEY: 78 return ""; // FIXME 79 } 80 break; 81 82 case B_BACKSPACE: 83 return "U+0008"; 84 case B_LEFT_ARROW: 85 return "Left"; 86 case B_RIGHT_ARROW: 87 return "Right"; 88 case B_UP_ARROW: 89 return "Up"; 90 case B_DOWN_ARROW: 91 return "Down"; 92 case B_INSERT: 93 return "Insert"; 94 case B_ENTER: 95 return "Enter"; 96 case B_DELETE: 97 return "U+007F"; 98 case B_HOME: 99 return "Home"; 100 case B_END: 101 return "End"; 102 case B_PAGE_UP: 103 return "PageUp"; 104 case B_PAGE_DOWN: 105 return "PageDown"; 106 case B_TAB: 107 return "U+0009"; 108 } 109 110 return String::format("U+%04X", toASCIIUpper(singleByte)); 111 } 112 113 static int windowsKeyCodeForKeyEvent(char singleByte, int keyCode) 114 { 115 switch (singleByte) { 116 case B_FUNCTION_KEY: 117 switch (keyCode) { 118 case B_F1_KEY: 119 return VK_F1; 120 case B_F2_KEY: 121 return VK_F2; 122 case B_F3_KEY: 123 return VK_F3; 124 case B_F4_KEY: 125 return VK_F4; 126 case B_F5_KEY: 127 return VK_F5; 128 case B_F6_KEY: 129 return VK_F6; 130 case B_F7_KEY: 131 return VK_F7; 132 case B_F8_KEY: 133 return VK_F8; 134 case B_F9_KEY: 135 return VK_F9; 136 case B_F10_KEY: 137 return VK_F10; 138 case B_F11_KEY: 139 return VK_F11; 140 case B_F12_KEY: 141 return VK_F12; 142 case B_PRINT_KEY: 143 return VK_PRINT; 144 case B_PAUSE_KEY: 145 return 0; // FIXME 146 case B_SCROLL_KEY: 147 return 0; // FIXME 148 } 149 break; 150 151 case B_BACKSPACE: 152 return VK_BACK; // (08) BACKSPACE key 153 case B_TAB: 154 return VK_TAB; // (09) TAB key 155 case B_RETURN: 156 return VK_RETURN; //(0D) Return key 157 case B_ESCAPE: 158 return VK_ESCAPE; // (1B) ESC key 159 case B_SPACE: 160 return VK_SPACE; // (20) SPACEBAR 161 case B_PAGE_UP: 162 return VK_PRIOR; // (21) PAGE UP key 163 case B_PAGE_DOWN: 164 return VK_NEXT; // (22) PAGE DOWN key 165 case B_END: 166 return VK_END; // (23) END key 167 case B_HOME: 168 return VK_HOME; // (24) HOME key 169 case B_LEFT_ARROW: 170 return VK_LEFT; // (25) LEFT ARROW key 171 case B_UP_ARROW: 172 return VK_UP; // (26) UP ARROW key 173 case B_RIGHT_ARROW: 174 return VK_RIGHT; // (27) RIGHT ARROW key 175 case B_DOWN_ARROW: 176 return VK_DOWN; // (28) DOWN ARROW key 177 case B_INSERT: 178 return VK_INSERT; // (2D) INS key 179 case B_DELETE: 180 return VK_DELETE; // (2E) DEL key 181 182 case '0': 183 case ')': 184 return VK_0; 185 case '1': 186 case '!': 187 return VK_1; 188 case '2': 189 case '@': 190 return VK_2; 191 case '3': 192 case '#': 193 return VK_3; 194 case '4': 195 case '$': 196 return VK_4; 197 case '5': 198 case '%': 199 return VK_5; 200 case '6': 201 case '^': 202 return VK_6; 203 case '7': 204 case '&': 205 return VK_7; 206 case '8': 207 case '*': 208 return VK_8; 209 case '9': 210 case '(': 211 return VK_9; 212 case 'a': 213 case 'A': 214 return VK_A; 215 case 'b': 216 case 'B': 217 return VK_B; 218 case 'c': 219 case 'C': 220 return VK_C; 221 case 'd': 222 case 'D': 223 return VK_D; 224 case 'e': 225 case 'E': 226 return VK_E; 227 case 'f': 228 case 'F': 229 return VK_F; 230 case 'g': 231 case 'G': 232 return VK_G; 233 case 'h': 234 case 'H': 235 return VK_H; 236 case 'i': 237 case 'I': 238 return VK_I; 239 case 'j': 240 case 'J': 241 return VK_J; 242 case 'k': 243 case 'K': 244 return VK_K; 245 case 'l': 246 case 'L': 247 return VK_L; 248 case 'm': 249 case 'M': 250 return VK_M; 251 case 'n': 252 case 'N': 253 return VK_N; 254 case 'o': 255 case 'O': 256 return VK_O; 257 case 'p': 258 case 'P': 259 return VK_P; 260 case 'q': 261 case 'Q': 262 return VK_Q; 263 case 'r': 264 case 'R': 265 return VK_R; 266 case 's': 267 case 'S': 268 return VK_S; 269 case 't': 270 case 'T': 271 return VK_T; 272 case 'u': 273 case 'U': 274 return VK_U; 275 case 'v': 276 case 'V': 277 return VK_V; 278 case 'w': 279 case 'W': 280 return VK_W; 281 case 'x': 282 case 'X': 283 return VK_X; 284 case 'y': 285 case 'Y': 286 return VK_Y; 287 case 'z': 288 case 'Z': 289 return VK_Z; 290 case ';': 291 case ':': 292 return VK_OEM_1; 293 case '+': 294 case '=': 295 return VK_OEM_PLUS; 296 case ',': 297 case '<': 298 return VK_OEM_COMMA; 299 case '-': 300 case '_': 301 return VK_OEM_MINUS; 302 case '.': 303 case '>': 304 return VK_OEM_PERIOD; 305 case '/': 306 case '?': 307 return VK_OEM_2; 308 case '`': 309 case '~': 310 return VK_OEM_3; 311 case '[': 312 case '{': 313 return VK_OEM_4; 314 case '\\': 315 case '|': 316 return VK_OEM_5; 317 case ']': 318 case '}': 319 return VK_OEM_6; 320 case '\'': 321 case '"': 322 return VK_OEM_7; 323 } 324 return singleByte; 325 } 326 327 PlatformKeyboardEvent::PlatformKeyboardEvent(BMessage* message) 328 : m_autoRepeat(false) 329 , m_isKeypad(false) 330 , m_shiftKey(false) 331 , m_ctrlKey(false) 332 , m_altKey(false) 333 , m_metaKey(false) 334 { 335 BString bytes = message->FindString("bytes"); 336 337 m_nativeVirtualKeyCode = message->FindInt32("key"); 338 339 m_text = String::fromUTF8(bytes.String(), bytes.Length()); 340 m_unmodifiedText = String(bytes.String(), bytes.Length()); 341 m_keyIdentifier = keyIdentifierForHaikuKeyCode(bytes.ByteAt(0), m_nativeVirtualKeyCode); 342 343 m_windowsVirtualKeyCode = windowsKeyCodeForKeyEvent(bytes.ByteAt(0), m_nativeVirtualKeyCode); 344 345 if (message->what == B_KEY_UP) 346 m_type = KeyUp; 347 else if (message->what == B_KEY_DOWN) 348 m_type = KeyDown; 349 350 int32 modifiers = message->FindInt32("modifiers"); 351 m_shiftKey = modifiers & B_SHIFT_KEY; 352 m_ctrlKey = modifiers & B_COMMAND_KEY; 353 m_altKey = modifiers & B_CONTROL_KEY; 354 m_metaKey = modifiers & B_OPTION_KEY; 355 } 356 357 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool backwardCompatibilityMode) 358 { 359 // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions. 360 ASSERT(m_type == KeyDown); 361 m_type = type; 362 363 if (backwardCompatibilityMode) 364 return; 365 366 if (type == RawKeyDown) { 367 m_text = String(); 368 m_unmodifiedText = String(); 369 } else { 370 m_keyIdentifier = String(); 371 m_windowsVirtualKeyCode = 0; 372 } 373 } 374 375 bool PlatformKeyboardEvent::currentCapsLockState() 376 { 377 return ::modifiers() & B_CAPS_LOCK; 378 } 379 380 void PlatformKeyboardEvent::getCurrentModifierState(bool& shiftKey, bool& ctrlKey, bool& altKey, bool& metaKey) 381 { 382 unit32 modifiers = ::modifiers(); 383 shiftKey = modifiers & B_SHIFT_KEY; 384 ctrlKey = modifiers & B_COMMAND_KEY; 385 altKey = modifiers & B_CONTROL_KEY; 386 metaKey = modifiers & B_OPTION_KEY; 387 } 388 389 } // namespace WebCore 390 391