1 /** 2 * Copyright (C) 2001 Peter Kelly (pmk (at) post.com) 3 * Copyright (C) 2001 Tobias Anton (anton (at) stud.fbi.fh-darmstadt.de) 4 * Copyright (C) 2006 Samuel Weinig (sam.weinig (at) gmail.com) 5 * Copyright (C) 2003, 2005, 2006, 2007 Apple Inc. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Library General Public 9 * License as published by the Free Software Foundation; either 10 * version 2 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Library General Public License for more details. 16 * 17 * You should have received a copy of the GNU Library General Public License 18 * along with this library; see the file COPYING.LIB. If not, write to 19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 20 * Boston, MA 02110-1301, USA. 21 */ 22 23 #include "config.h" 24 #include "core/dom/KeyboardEvent.h" 25 26 #include "core/dom/EventNames.h" 27 #include "core/platform/PlatformKeyboardEvent.h" 28 #include "core/platform/WindowsKeyboardCodes.h" 29 30 namespace WebCore { 31 32 static inline const AtomicString& eventTypeForKeyboardEventType(PlatformEvent::Type type) 33 { 34 switch (type) { 35 case PlatformEvent::KeyUp: 36 return eventNames().keyupEvent; 37 case PlatformEvent::RawKeyDown: 38 return eventNames().keydownEvent; 39 case PlatformEvent::Char: 40 return eventNames().keypressEvent; 41 case PlatformEvent::KeyDown: 42 // The caller should disambiguate the combined event into RawKeyDown or Char events. 43 break; 44 default: 45 break; 46 } 47 ASSERT_NOT_REACHED(); 48 return eventNames().keydownEvent; 49 } 50 51 static inline int windowsVirtualKeyCodeWithoutLocation(int keycode) 52 { 53 switch (keycode) { 54 case VK_LCONTROL: 55 case VK_RCONTROL: 56 return VK_CONTROL; 57 case VK_LSHIFT: 58 case VK_RSHIFT: 59 return VK_SHIFT; 60 case VK_LMENU: 61 case VK_RMENU: 62 return VK_MENU; 63 default: 64 return keycode; 65 } 66 } 67 68 static inline KeyboardEvent::KeyLocationCode keyLocationCode(const PlatformKeyboardEvent& key) 69 { 70 if (key.isKeypad()) 71 return KeyboardEvent::DOM_KEY_LOCATION_NUMPAD; 72 73 // FIXME: Support DOM_KEY_LOCATION_MOBILE & DOM_KEY_LOCATION_JOYSTICK (crbug.com/265446). 74 switch (key.windowsVirtualKeyCode()) { 75 case VK_LCONTROL: 76 case VK_LSHIFT: 77 case VK_LMENU: 78 case VK_LWIN: 79 return KeyboardEvent::DOM_KEY_LOCATION_LEFT; 80 case VK_RCONTROL: 81 case VK_RSHIFT: 82 case VK_RMENU: 83 case VK_RWIN: 84 return KeyboardEvent::DOM_KEY_LOCATION_RIGHT; 85 default: 86 return KeyboardEvent::DOM_KEY_LOCATION_STANDARD; 87 } 88 } 89 90 KeyboardEventInit::KeyboardEventInit() 91 : location(0) 92 , ctrlKey(false) 93 , altKey(false) 94 , shiftKey(false) 95 , metaKey(false) 96 { 97 } 98 99 KeyboardEvent::KeyboardEvent() 100 : m_location(DOM_KEY_LOCATION_STANDARD) 101 , m_altGraphKey(false) 102 { 103 ScriptWrappable::init(this); 104 } 105 106 KeyboardEvent::KeyboardEvent(const PlatformKeyboardEvent& key, AbstractView* view) 107 : UIEventWithKeyState(eventTypeForKeyboardEventType(key.type()), 108 true, true, view, 0, key.ctrlKey(), key.altKey(), key.shiftKey(), key.metaKey()) 109 , m_keyEvent(adoptPtr(new PlatformKeyboardEvent(key))) 110 , m_keyIdentifier(key.keyIdentifier()) 111 , m_location(keyLocationCode(key)) 112 , m_altGraphKey(false) 113 { 114 ScriptWrappable::init(this); 115 } 116 117 KeyboardEvent::KeyboardEvent(const AtomicString& eventType, const KeyboardEventInit& initializer) 118 : UIEventWithKeyState(eventType, initializer.bubbles, initializer.cancelable, initializer.view, initializer.detail, initializer.ctrlKey, initializer.altKey, initializer.shiftKey, initializer.metaKey) 119 , m_keyIdentifier(initializer.keyIdentifier) 120 , m_location(initializer.location) 121 , m_altGraphKey(false) 122 { 123 ScriptWrappable::init(this); 124 } 125 126 KeyboardEvent::KeyboardEvent(const AtomicString& eventType, bool canBubble, bool cancelable, AbstractView *view, 127 const String &keyIdentifier, unsigned location, 128 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey) 129 : UIEventWithKeyState(eventType, canBubble, cancelable, view, 0, ctrlKey, altKey, shiftKey, metaKey) 130 , m_keyIdentifier(keyIdentifier) 131 , m_location(location) 132 , m_altGraphKey(altGraphKey) 133 { 134 ScriptWrappable::init(this); 135 } 136 137 KeyboardEvent::~KeyboardEvent() 138 { 139 } 140 141 void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view, 142 const String &keyIdentifier, unsigned location, 143 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey) 144 { 145 if (dispatched()) 146 return; 147 148 initUIEvent(type, canBubble, cancelable, view, 0); 149 150 m_keyIdentifier = keyIdentifier; 151 m_location = location; 152 m_ctrlKey = ctrlKey; 153 m_shiftKey = shiftKey; 154 m_altKey = altKey; 155 m_metaKey = metaKey; 156 m_altGraphKey = altGraphKey; 157 } 158 159 bool KeyboardEvent::getModifierState(const String& keyIdentifier) const 160 { 161 // FIXME: The following keyIdentifiers are not supported yet (crbug.com/265458): 162 // "AltGraph", "CapsLock", "Fn", "NumLock", "ScrollLock", "SymbolLock", "OS". 163 if (keyIdentifier == "Control") 164 return ctrlKey(); 165 if (keyIdentifier == "Shift") 166 return shiftKey(); 167 if (keyIdentifier == "Alt") 168 return altKey(); 169 if (keyIdentifier == "Meta") 170 return metaKey(); 171 return false; 172 } 173 174 int KeyboardEvent::keyCode() const 175 { 176 // IE: virtual key code for keyup/keydown, character code for keypress 177 // Firefox: virtual key code for keyup/keydown, zero for keypress 178 // We match IE. 179 if (!m_keyEvent) 180 return 0; 181 if (type() == eventNames().keydownEvent || type() == eventNames().keyupEvent) 182 return windowsVirtualKeyCodeWithoutLocation(m_keyEvent->windowsVirtualKeyCode()); 183 184 return charCode(); 185 } 186 187 int KeyboardEvent::charCode() const 188 { 189 // IE: not supported 190 // Firefox: 0 for keydown/keyup events, character code for keypress 191 // We match Firefox 192 193 if (!m_keyEvent || (type() != eventNames().keypressEvent)) 194 return 0; 195 String text = m_keyEvent->text(); 196 return static_cast<int>(text.characterStartingAt(0)); 197 } 198 199 const AtomicString& KeyboardEvent::interfaceName() const 200 { 201 return eventNames().interfaceForKeyboardEvent; 202 } 203 204 bool KeyboardEvent::isKeyboardEvent() const 205 { 206 return true; 207 } 208 209 int KeyboardEvent::which() const 210 { 211 // Netscape's "which" returns a virtual key code for keydown and keyup, and a character code for keypress. 212 // That's exactly what IE's "keyCode" returns. So they are the same for keyboard events. 213 return keyCode(); 214 } 215 216 KeyboardEvent* findKeyboardEvent(Event* event) 217 { 218 for (Event* e = event; e; e = e->underlyingEvent()) { 219 if (e->isKeyboardEvent()) 220 return toKeyboardEvent(e); 221 } 222 return 0; 223 } 224 225 PassRefPtr<KeyboardEventDispatchMediator> KeyboardEventDispatchMediator::create(PassRefPtr<KeyboardEvent> event) 226 { 227 return adoptRef(new KeyboardEventDispatchMediator(event)); 228 } 229 230 KeyboardEventDispatchMediator::KeyboardEventDispatchMediator(PassRefPtr<KeyboardEvent> event) 231 : EventDispatchMediator(event) 232 { 233 } 234 235 bool KeyboardEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) const 236 { 237 // Make sure not to return true if we already took default action while handling the event. 238 return EventDispatchMediator::dispatchEvent(dispatcher) && !event()->defaultHandled(); 239 } 240 241 } // namespace WebCore 242