1 /**
2 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
4 * Copyright (C) 2006 Samuel Weinig (sam.weinig@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/events/KeyboardEvent.h"
25
26 #include "platform/PlatformKeyboardEvent.h"
27 #include "platform/WindowsKeyboardCodes.h"
28
29 namespace WebCore {
30
eventTypeForKeyboardEventType(PlatformEvent::Type type)31 static inline const AtomicString& eventTypeForKeyboardEventType(PlatformEvent::Type type)
32 {
33 switch (type) {
34 case PlatformEvent::KeyUp:
35 return EventTypeNames::keyup;
36 case PlatformEvent::RawKeyDown:
37 return EventTypeNames::keydown;
38 case PlatformEvent::Char:
39 return EventTypeNames::keypress;
40 case PlatformEvent::KeyDown:
41 // The caller should disambiguate the combined event into RawKeyDown or Char events.
42 break;
43 default:
44 break;
45 }
46 ASSERT_NOT_REACHED();
47 return EventTypeNames::keydown;
48 }
49
windowsVirtualKeyCodeWithoutLocation(int keycode)50 static inline int windowsVirtualKeyCodeWithoutLocation(int keycode)
51 {
52 switch (keycode) {
53 case VK_LCONTROL:
54 case VK_RCONTROL:
55 return VK_CONTROL;
56 case VK_LSHIFT:
57 case VK_RSHIFT:
58 return VK_SHIFT;
59 case VK_LMENU:
60 case VK_RMENU:
61 return VK_MENU;
62 default:
63 return keycode;
64 }
65 }
66
keyLocationCode(const PlatformKeyboardEvent & key)67 static inline KeyboardEvent::KeyLocationCode keyLocationCode(const PlatformKeyboardEvent& key)
68 {
69 if (key.isKeypad())
70 return KeyboardEvent::DOM_KEY_LOCATION_NUMPAD;
71
72 switch (key.windowsVirtualKeyCode()) {
73 case VK_LCONTROL:
74 case VK_LSHIFT:
75 case VK_LMENU:
76 case VK_LWIN:
77 return KeyboardEvent::DOM_KEY_LOCATION_LEFT;
78 case VK_RCONTROL:
79 case VK_RSHIFT:
80 case VK_RMENU:
81 case VK_RWIN:
82 return KeyboardEvent::DOM_KEY_LOCATION_RIGHT;
83 default:
84 return KeyboardEvent::DOM_KEY_LOCATION_STANDARD;
85 }
86 }
87
KeyboardEventInit()88 KeyboardEventInit::KeyboardEventInit()
89 : location(0)
90 , ctrlKey(false)
91 , altKey(false)
92 , shiftKey(false)
93 , metaKey(false)
94 , repeat(false)
95 {
96 }
97
KeyboardEvent()98 KeyboardEvent::KeyboardEvent()
99 : m_location(DOM_KEY_LOCATION_STANDARD)
100 , m_altGraphKey(false)
101 , m_isAutoRepeat(false)
102 {
103 ScriptWrappable::init(this);
104 }
105
KeyboardEvent(const PlatformKeyboardEvent & key,AbstractView * view)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 , m_isAutoRepeat(key.isAutoRepeat())
114 {
115 ScriptWrappable::init(this);
116 }
117
KeyboardEvent(const AtomicString & eventType,const KeyboardEventInit & initializer)118 KeyboardEvent::KeyboardEvent(const AtomicString& eventType, const KeyboardEventInit& initializer)
119 : UIEventWithKeyState(eventType, initializer.bubbles, initializer.cancelable, initializer.view, initializer.detail, initializer.ctrlKey, initializer.altKey, initializer.shiftKey, initializer.metaKey)
120 , m_keyIdentifier(initializer.keyIdentifier)
121 , m_location(initializer.location)
122 , m_altGraphKey(false)
123 , m_isAutoRepeat(initializer.repeat)
124 {
125 ScriptWrappable::init(this);
126 }
127
KeyboardEvent(const AtomicString & eventType,bool canBubble,bool cancelable,AbstractView * view,const String & keyIdentifier,unsigned location,bool ctrlKey,bool altKey,bool shiftKey,bool metaKey,bool altGraphKey)128 KeyboardEvent::KeyboardEvent(const AtomicString& eventType, bool canBubble, bool cancelable, AbstractView *view,
129 const String &keyIdentifier, unsigned location,
130 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
131 : UIEventWithKeyState(eventType, canBubble, cancelable, view, 0, ctrlKey, altKey, shiftKey, metaKey)
132 , m_keyIdentifier(keyIdentifier)
133 , m_location(location)
134 , m_altGraphKey(altGraphKey)
135 , m_isAutoRepeat(false)
136 {
137 ScriptWrappable::init(this);
138 }
139
~KeyboardEvent()140 KeyboardEvent::~KeyboardEvent()
141 {
142 }
143
initKeyboardEvent(const AtomicString & type,bool canBubble,bool cancelable,AbstractView * view,const String & keyIdentifier,unsigned location,bool ctrlKey,bool altKey,bool shiftKey,bool metaKey,bool altGraphKey)144 void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view,
145 const String &keyIdentifier, unsigned location,
146 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
147 {
148 if (dispatched())
149 return;
150
151 initUIEvent(type, canBubble, cancelable, view, 0);
152
153 m_keyIdentifier = keyIdentifier;
154 m_location = location;
155 m_ctrlKey = ctrlKey;
156 m_shiftKey = shiftKey;
157 m_altKey = altKey;
158 m_metaKey = metaKey;
159 m_altGraphKey = altGraphKey;
160 }
161
getModifierState(const String & keyIdentifier) const162 bool KeyboardEvent::getModifierState(const String& keyIdentifier) const
163 {
164 // FIXME: The following keyIdentifiers are not supported yet (crbug.com/265458):
165 // "AltGraph", "CapsLock", "Fn", "NumLock", "ScrollLock", "SymbolLock", "OS".
166 if (keyIdentifier == "Control")
167 return ctrlKey();
168 if (keyIdentifier == "Shift")
169 return shiftKey();
170 if (keyIdentifier == "Alt")
171 return altKey();
172 if (keyIdentifier == "Meta")
173 return metaKey();
174 return false;
175 }
176
keyCode() const177 int KeyboardEvent::keyCode() const
178 {
179 // IE: virtual key code for keyup/keydown, character code for keypress
180 // Firefox: virtual key code for keyup/keydown, zero for keypress
181 // We match IE.
182 if (!m_keyEvent)
183 return 0;
184 if (type() == EventTypeNames::keydown || type() == EventTypeNames::keyup)
185 return windowsVirtualKeyCodeWithoutLocation(m_keyEvent->windowsVirtualKeyCode());
186
187 return charCode();
188 }
189
charCode() const190 int KeyboardEvent::charCode() const
191 {
192 // IE: not supported
193 // Firefox: 0 for keydown/keyup events, character code for keypress
194 // We match Firefox
195
196 if (!m_keyEvent || (type() != EventTypeNames::keypress))
197 return 0;
198 String text = m_keyEvent->text();
199 return static_cast<int>(text.characterStartingAt(0));
200 }
201
interfaceName() const202 const AtomicString& KeyboardEvent::interfaceName() const
203 {
204 return EventNames::KeyboardEvent;
205 }
206
isKeyboardEvent() const207 bool KeyboardEvent::isKeyboardEvent() const
208 {
209 return true;
210 }
211
which() const212 int KeyboardEvent::which() const
213 {
214 // Netscape's "which" returns a virtual key code for keydown and keyup, and a character code for keypress.
215 // That's exactly what IE's "keyCode" returns. So they are the same for keyboard events.
216 return keyCode();
217 }
218
trace(Visitor * visitor)219 void KeyboardEvent::trace(Visitor* visitor)
220 {
221 UIEventWithKeyState::trace(visitor);
222 }
223
create(PassRefPtrWillBeRawPtr<KeyboardEvent> event)224 PassRefPtrWillBeRawPtr<KeyboardEventDispatchMediator> KeyboardEventDispatchMediator::create(PassRefPtrWillBeRawPtr<KeyboardEvent> event)
225 {
226 return adoptRefWillBeNoop(new KeyboardEventDispatchMediator(event));
227 }
228
KeyboardEventDispatchMediator(PassRefPtrWillBeRawPtr<KeyboardEvent> event)229 KeyboardEventDispatchMediator::KeyboardEventDispatchMediator(PassRefPtrWillBeRawPtr<KeyboardEvent> event)
230 : EventDispatchMediator(event)
231 {
232 }
233
dispatchEvent(EventDispatcher * dispatcher) const234 bool KeyboardEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) const
235 {
236 // Make sure not to return true if we already took default action while handling the event.
237 return EventDispatchMediator::dispatchEvent(dispatcher) && !event()->defaultHandled();
238 }
239
240 } // namespace WebCore
241