1 /* 2 * Copyright (C) 2007 Ryan Leavengood <leavengood (at) gmail.com> 3 * Copyright (C) 2008 Andrea Anzani <andrea.anzani (at) gmail.com> 4 * 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "config.h" 30 #include "PlatformKeyboardEvent.h" 31 32 #include "KeyboardCodes.h" 33 #include "NotImplemented.h" 34 #include <InterfaceDefs.h> 35 #include <Message.h> 36 #include <String.h> 37 38 39 namespace WebCore { 40 41 static String keyIdentifierForHaikuKeyCode(char singleByte, int keyCode) 42 { 43 switch (singleByte) { 44 case B_FUNCTION_KEY: 45 46 switch (keyCode) { 47 case B_F1_KEY: 48 return "F1"; 49 case B_F2_KEY: 50 return "F2"; 51 case B_F3_KEY: 52 return "F3"; 53 case B_F4_KEY: 54 return "F4"; 55 case B_F5_KEY: 56 return "F5"; 57 case B_F6_KEY: 58 return "F6"; 59 case B_F7_KEY: 60 return "F7"; 61 case B_F8_KEY: 62 return "F8"; 63 case B_F9_KEY: 64 return "F9"; 65 case B_F10_KEY: 66 return "F10"; 67 case B_F11_KEY: 68 return "F11"; 69 case B_F12_KEY: 70 return "F12"; 71 case B_PRINT_KEY: 72 return "Print"; 73 case B_PAUSE_KEY: 74 return "Pause"; 75 case B_SCROLL_KEY: 76 return ""; // FIXME 77 } 78 break; 79 80 case B_BACKSPACE: 81 return "U+0009"; 82 case B_LEFT_ARROW: 83 return "Left"; 84 case B_RIGHT_ARROW: 85 return "Right"; 86 case B_UP_ARROW: 87 return "Up"; 88 case B_DOWN_ARROW: 89 return "Down"; 90 case B_INSERT: 91 return "Insert"; 92 case B_ENTER: 93 return "Enter"; 94 case B_DELETE: 95 return "U+007F"; 96 case B_HOME: 97 return "Home"; 98 case B_END: 99 return "End"; 100 case B_PAGE_UP: 101 return "PageUp"; 102 case B_PAGE_DOWN: 103 return "PageDown"; 104 case B_TAB: 105 return "U+0009"; 106 } 107 108 return String::format("U+%04X", toASCIIUpper(keyCode)); 109 } 110 111 static int windowsKeyCodeForKeyEvent(char singleByte) 112 { 113 switch (singleByte) { 114 case B_BACKSPACE: 115 return VK_BACK; // (08) BACKSPACE key 116 case B_TAB: 117 return VK_TAB; // (09) TAB key 118 case B_RETURN: 119 return VK_RETURN; //(0D) Return key 120 case B_ESCAPE: 121 return VK_ESCAPE; // (1B) ESC key 122 case B_SPACE: 123 return VK_SPACE; // (20) SPACEBAR 124 case B_PAGE_UP: 125 return VK_PRIOR; // (21) PAGE UP key 126 case B_PAGE_DOWN: 127 return VK_NEXT; // (22) PAGE DOWN key 128 case B_END: 129 return VK_END; // (23) END key 130 case B_HOME: 131 return VK_HOME; // (24) HOME key 132 case B_LEFT_ARROW: 133 return VK_LEFT; // (25) LEFT ARROW key 134 case B_UP_ARROW: 135 return VK_UP; // (26) UP ARROW key 136 case B_RIGHT_ARROW: 137 return VK_RIGHT; // (27) RIGHT ARROW key 138 case B_DOWN_ARROW: 139 return VK_DOWN; // (28) DOWN ARROW key 140 case B_INSERT: 141 return VK_INSERT; // (2D) INS key 142 case B_DELETE: 143 return VK_DELETE; // (2E) DEL key 144 case 0x2e: 145 default: 146 return 0; 147 } 148 } 149 150 PlatformKeyboardEvent::PlatformKeyboardEvent(BMessage* message) 151 : m_autoRepeat(false) 152 , m_ctrlKey(false) 153 , m_altKey(false) 154 , m_metaKey(false) 155 , m_isKeypad(false) 156 , m_shiftKey(false) 157 { 158 BString bytes = message->FindString("bytes"); 159 160 m_text = String::fromUTF8(bytes.String(), bytes.Length()); 161 m_unmodifiedText = String(bytes.String(), bytes.Length()); 162 m_keyIdentifier = keyIdentifierForHaikuKeyCode(bytes.ByteAt(0), message->FindInt32("key")); 163 164 if (message->what == B_KEY_UP) 165 m_type = KeyUp; 166 else if (message->what == B_KEY_DOWN) 167 m_type = KeyDown; 168 169 m_windowsVirtualKeyCode = windowsKeyCodeForKeyEvent(bytes.ByteAt(0)); 170 } 171 172 void PlatformKeyboardEvent::disambiguateKeyDownEvent(Type type, bool) 173 { 174 // Can only change type from KeyDown to RawKeyDown or Char, as we lack information for other conversions. 175 ASSERT(m_type == KeyDown); 176 m_type = type; 177 if (type == RawKeyDown) { 178 m_text = String(); 179 m_unmodifiedText = String(); 180 } else { 181 m_keyIdentifier = String(); 182 m_windowsVirtualKeyCode = 0; 183 } 184 } 185 186 bool PlatformKeyboardEvent::currentCapsLockState() 187 { 188 notImplemented(); 189 return false; 190 } 191 192 } // namespace WebCore 193 194