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 "platform/PlatformKeyboardEvent.h"
27 #include "platform/WindowsKeyboardCodes.h"
31 static inline const AtomicString& eventTypeForKeyboardEventType(PlatformEvent::Type 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.
47 return EventTypeNames::keydown;
50 static inline int windowsVirtualKeyCodeWithoutLocation(int keycode)
67 static inline KeyboardEvent::KeyLocationCode keyLocationCode(const PlatformKeyboardEvent& key)
70 return KeyboardEvent::DOM_KEY_LOCATION_NUMPAD;
72 switch (key.windowsVirtualKeyCode()) {
77 return KeyboardEvent::DOM_KEY_LOCATION_LEFT;
82 return KeyboardEvent::DOM_KEY_LOCATION_RIGHT;
84 return KeyboardEvent::DOM_KEY_LOCATION_STANDARD;
88 KeyboardEventInit::KeyboardEventInit()
98 KeyboardEvent::KeyboardEvent()
99 : m_location(DOM_KEY_LOCATION_STANDARD)
100 , m_isAutoRepeat(false)
104 KeyboardEvent::KeyboardEvent(const PlatformKeyboardEvent& key, AbstractView* view)
105 : UIEventWithKeyState(eventTypeForKeyboardEventType(key.type()),
106 true, true, view, 0, key.ctrlKey(), key.altKey(), key.shiftKey(), key.metaKey())
107 , m_keyEvent(adoptPtr(new PlatformKeyboardEvent(key)))
108 , m_keyIdentifier(key.keyIdentifier())
109 , m_location(keyLocationCode(key))
110 , m_isAutoRepeat(key.isAutoRepeat())
114 KeyboardEvent::KeyboardEvent(const AtomicString& eventType, const KeyboardEventInit& initializer)
115 : UIEventWithKeyState(eventType, initializer.bubbles, initializer.cancelable, initializer.view, initializer.detail, initializer.ctrlKey, initializer.altKey, initializer.shiftKey, initializer.metaKey)
116 , m_keyIdentifier(initializer.keyIdentifier)
117 , m_location(initializer.location)
118 , m_isAutoRepeat(initializer.repeat)
122 KeyboardEvent::KeyboardEvent(const AtomicString& eventType, bool canBubble, bool cancelable, AbstractView *view,
123 const String &keyIdentifier, unsigned location,
124 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
125 : UIEventWithKeyState(eventType, canBubble, cancelable, view, 0, ctrlKey, altKey, shiftKey, metaKey)
126 , m_keyIdentifier(keyIdentifier)
127 , m_location(location)
128 , m_isAutoRepeat(false)
132 KeyboardEvent::~KeyboardEvent()
136 void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view,
137 const String &keyIdentifier, unsigned location,
138 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
143 initUIEvent(type, canBubble, cancelable, view, 0);
145 m_keyIdentifier = keyIdentifier;
146 m_location = location;
148 m_shiftKey = shiftKey;
153 bool KeyboardEvent::getModifierState(const String& keyIdentifier) const
155 // FIXME: The following keyIdentifiers are not supported yet (crbug.com/265458):
156 // "AltGraph", "CapsLock", "Fn", "NumLock", "ScrollLock", "SymbolLock", "OS".
157 if (keyIdentifier == "Control")
159 if (keyIdentifier == "Shift")
161 if (keyIdentifier == "Alt")
163 if (keyIdentifier == "Meta")
168 int KeyboardEvent::keyCode() const
170 // IE: virtual key code for keyup/keydown, character code for keypress
171 // Firefox: virtual key code for keyup/keydown, zero for keypress
175 if (type() == EventTypeNames::keydown || type() == EventTypeNames::keyup)
176 return windowsVirtualKeyCodeWithoutLocation(m_keyEvent->windowsVirtualKeyCode());
181 int KeyboardEvent::charCode() const
184 // Firefox: 0 for keydown/keyup events, character code for keypress
187 if (!m_keyEvent || (type() != EventTypeNames::keypress))
189 String text = m_keyEvent->text();
190 return static_cast<int>(text.characterStartingAt(0));
193 const AtomicString& KeyboardEvent::interfaceName() const
195 return EventNames::KeyboardEvent;
198 bool KeyboardEvent::isKeyboardEvent() const
203 int KeyboardEvent::which() const
205 // Netscape's "which" returns a virtual key code for keydown and keyup, and a character code for keypress.
206 // That's exactly what IE's "keyCode" returns. So they are the same for keyboard events.
210 void KeyboardEvent::trace(Visitor* visitor)
212 UIEventWithKeyState::trace(visitor);
215 PassRefPtrWillBeRawPtr<KeyboardEventDispatchMediator> KeyboardEventDispatchMediator::create(PassRefPtrWillBeRawPtr<KeyboardEvent> event)
217 return adoptRefWillBeNoop(new KeyboardEventDispatchMediator(event));
220 KeyboardEventDispatchMediator::KeyboardEventDispatchMediator(PassRefPtrWillBeRawPtr<KeyboardEvent> event)
221 : EventDispatchMediator(event)
225 bool KeyboardEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) const
227 // Make sure not to return true if we already took default action while handling the event.
228 return EventDispatchMediator::dispatchEvent(dispatcher) && !event()->defaultHandled();