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.
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.
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.
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.
24 #include "core/events/KeyboardEvent.h"
26 #include "core/events/ThreadLocalEventNames.h"
27 #include "platform/PlatformKeyboardEvent.h"
28 #include "platform/WindowsKeyboardCodes.h"
32 static inline const AtomicString& eventTypeForKeyboardEventType(PlatformEvent::Type type)
35 case PlatformEvent::KeyUp:
36 return EventTypeNames::keyup;
37 case PlatformEvent::RawKeyDown:
38 return EventTypeNames::keydown;
39 case PlatformEvent::Char:
40 return EventTypeNames::keypress;
41 case PlatformEvent::KeyDown:
42 // The caller should disambiguate the combined event into RawKeyDown or Char events.
48 return EventTypeNames::keydown;
51 static inline int windowsVirtualKeyCodeWithoutLocation(int keycode)
68 static inline KeyboardEvent::KeyLocationCode keyLocationCode(const PlatformKeyboardEvent& key)
71 return KeyboardEvent::DOM_KEY_LOCATION_NUMPAD;
73 switch (key.windowsVirtualKeyCode()) {
78 return KeyboardEvent::DOM_KEY_LOCATION_LEFT;
83 return KeyboardEvent::DOM_KEY_LOCATION_RIGHT;
85 return KeyboardEvent::DOM_KEY_LOCATION_STANDARD;
89 KeyboardEventInit::KeyboardEventInit()
99 KeyboardEvent::KeyboardEvent()
100 : m_location(DOM_KEY_LOCATION_STANDARD)
101 , m_altGraphKey(false)
102 , m_isAutoRepeat(false)
104 ScriptWrappable::init(this);
107 KeyboardEvent::KeyboardEvent(const PlatformKeyboardEvent& key, AbstractView* view)
108 : UIEventWithKeyState(eventTypeForKeyboardEventType(key.type()),
109 true, true, view, 0, key.ctrlKey(), key.altKey(), key.shiftKey(), key.metaKey())
110 , m_keyEvent(adoptPtr(new PlatformKeyboardEvent(key)))
111 , m_keyIdentifier(key.keyIdentifier())
112 , m_location(keyLocationCode(key))
113 , m_altGraphKey(false)
114 , m_isAutoRepeat(key.isAutoRepeat())
116 ScriptWrappable::init(this);
119 KeyboardEvent::KeyboardEvent(const AtomicString& eventType, const KeyboardEventInit& initializer)
120 : UIEventWithKeyState(eventType, initializer.bubbles, initializer.cancelable, initializer.view, initializer.detail, initializer.ctrlKey, initializer.altKey, initializer.shiftKey, initializer.metaKey)
121 , m_keyIdentifier(initializer.keyIdentifier)
122 , m_location(initializer.location)
123 , m_altGraphKey(false)
124 , m_isAutoRepeat(initializer.repeat)
126 ScriptWrappable::init(this);
129 KeyboardEvent::KeyboardEvent(const AtomicString& eventType, bool canBubble, bool cancelable, AbstractView *view,
130 const String &keyIdentifier, unsigned location,
131 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
132 : UIEventWithKeyState(eventType, canBubble, cancelable, view, 0, ctrlKey, altKey, shiftKey, metaKey)
133 , m_keyIdentifier(keyIdentifier)
134 , m_location(location)
135 , m_altGraphKey(altGraphKey)
136 , m_isAutoRepeat(false)
138 ScriptWrappable::init(this);
141 KeyboardEvent::~KeyboardEvent()
145 void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view,
146 const String &keyIdentifier, unsigned location,
147 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
152 initUIEvent(type, canBubble, cancelable, view, 0);
154 m_keyIdentifier = keyIdentifier;
155 m_location = location;
157 m_shiftKey = shiftKey;
160 m_altGraphKey = altGraphKey;
163 bool KeyboardEvent::getModifierState(const String& keyIdentifier) const
165 // FIXME: The following keyIdentifiers are not supported yet (crbug.com/265458):
166 // "AltGraph", "CapsLock", "Fn", "NumLock", "ScrollLock", "SymbolLock", "OS".
167 if (keyIdentifier == "Control")
169 if (keyIdentifier == "Shift")
171 if (keyIdentifier == "Alt")
173 if (keyIdentifier == "Meta")
178 int KeyboardEvent::keyCode() const
180 // IE: virtual key code for keyup/keydown, character code for keypress
181 // Firefox: virtual key code for keyup/keydown, zero for keypress
185 if (type() == EventTypeNames::keydown || type() == EventTypeNames::keyup)
186 return windowsVirtualKeyCodeWithoutLocation(m_keyEvent->windowsVirtualKeyCode());
191 int KeyboardEvent::charCode() const
194 // Firefox: 0 for keydown/keyup events, character code for keypress
197 if (!m_keyEvent || (type() != EventTypeNames::keypress))
199 String text = m_keyEvent->text();
200 return static_cast<int>(text.characterStartingAt(0));
203 const AtomicString& KeyboardEvent::interfaceName() const
205 return EventNames::KeyboardEvent;
208 bool KeyboardEvent::isKeyboardEvent() const
213 int KeyboardEvent::which() const
215 // Netscape's "which" returns a virtual key code for keydown and keyup, and a character code for keypress.
216 // That's exactly what IE's "keyCode" returns. So they are the same for keyboard events.
220 PassRefPtr<KeyboardEventDispatchMediator> KeyboardEventDispatchMediator::create(PassRefPtr<KeyboardEvent> event)
222 return adoptRef(new KeyboardEventDispatchMediator(event));
225 KeyboardEventDispatchMediator::KeyboardEventDispatchMediator(PassRefPtr<KeyboardEvent> event)
226 : EventDispatchMediator(event)
230 bool KeyboardEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) const
232 // Make sure not to return true if we already took default action while handling the event.
233 return EventDispatchMediator::dispatchEvent(dispatcher) && !event()->defaultHandled();
236 } // namespace WebCore