Home | History | Annotate | Download | only in events
      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/events/KeyboardEvent.h"
     25 
     26 #include "core/events/ThreadLocalEventNames.h"
     27 #include "platform/PlatformKeyboardEvent.h"
     28 #include "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 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.
     43             break;
     44         default:
     45             break;
     46     }
     47     ASSERT_NOT_REACHED();
     48     return EventTypeNames::keydown;
     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     switch (key.windowsVirtualKeyCode()) {
     74     case VK_LCONTROL:
     75     case VK_LSHIFT:
     76     case VK_LMENU:
     77     case VK_LWIN:
     78         return KeyboardEvent::DOM_KEY_LOCATION_LEFT;
     79     case VK_RCONTROL:
     80     case VK_RSHIFT:
     81     case VK_RMENU:
     82     case VK_RWIN:
     83         return KeyboardEvent::DOM_KEY_LOCATION_RIGHT;
     84     default:
     85         return KeyboardEvent::DOM_KEY_LOCATION_STANDARD;
     86     }
     87 }
     88 
     89 KeyboardEventInit::KeyboardEventInit()
     90     : location(0)
     91     , ctrlKey(false)
     92     , altKey(false)
     93     , shiftKey(false)
     94     , metaKey(false)
     95     , repeat(false)
     96 {
     97 }
     98 
     99 KeyboardEvent::KeyboardEvent()
    100     : m_location(DOM_KEY_LOCATION_STANDARD)
    101     , m_altGraphKey(false)
    102     , m_isAutoRepeat(false)
    103 {
    104     ScriptWrappable::init(this);
    105 }
    106 
    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())
    115 {
    116     ScriptWrappable::init(this);
    117 }
    118 
    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)
    125 {
    126     ScriptWrappable::init(this);
    127 }
    128 
    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)
    137 {
    138     ScriptWrappable::init(this);
    139 }
    140 
    141 KeyboardEvent::~KeyboardEvent()
    142 {
    143 }
    144 
    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)
    148 {
    149     if (dispatched())
    150         return;
    151 
    152     initUIEvent(type, canBubble, cancelable, view, 0);
    153 
    154     m_keyIdentifier = keyIdentifier;
    155     m_location = location;
    156     m_ctrlKey = ctrlKey;
    157     m_shiftKey = shiftKey;
    158     m_altKey = altKey;
    159     m_metaKey = metaKey;
    160     m_altGraphKey = altGraphKey;
    161 }
    162 
    163 bool KeyboardEvent::getModifierState(const String& keyIdentifier) const
    164 {
    165     // FIXME: The following keyIdentifiers are not supported yet (crbug.com/265458):
    166     // "AltGraph", "CapsLock", "Fn", "NumLock", "ScrollLock", "SymbolLock", "OS".
    167     if (keyIdentifier == "Control")
    168         return ctrlKey();
    169     if (keyIdentifier == "Shift")
    170         return shiftKey();
    171     if (keyIdentifier == "Alt")
    172         return altKey();
    173     if (keyIdentifier == "Meta")
    174         return metaKey();
    175     return false;
    176 }
    177 
    178 int KeyboardEvent::keyCode() const
    179 {
    180     // IE: virtual key code for keyup/keydown, character code for keypress
    181     // Firefox: virtual key code for keyup/keydown, zero for keypress
    182     // We match IE.
    183     if (!m_keyEvent)
    184         return 0;
    185     if (type() == EventTypeNames::keydown || type() == EventTypeNames::keyup)
    186         return windowsVirtualKeyCodeWithoutLocation(m_keyEvent->windowsVirtualKeyCode());
    187 
    188     return charCode();
    189 }
    190 
    191 int KeyboardEvent::charCode() const
    192 {
    193     // IE: not supported
    194     // Firefox: 0 for keydown/keyup events, character code for keypress
    195     // We match Firefox
    196 
    197     if (!m_keyEvent || (type() != EventTypeNames::keypress))
    198         return 0;
    199     String text = m_keyEvent->text();
    200     return static_cast<int>(text.characterStartingAt(0));
    201 }
    202 
    203 const AtomicString& KeyboardEvent::interfaceName() const
    204 {
    205     return EventNames::KeyboardEvent;
    206 }
    207 
    208 bool KeyboardEvent::isKeyboardEvent() const
    209 {
    210     return true;
    211 }
    212 
    213 int KeyboardEvent::which() const
    214 {
    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.
    217     return keyCode();
    218 }
    219 
    220 KeyboardEvent* findKeyboardEvent(Event* event)
    221 {
    222     for (Event* e = event; e; e = e->underlyingEvent()) {
    223         if (e->isKeyboardEvent())
    224             return toKeyboardEvent(e);
    225     }
    226     return 0;
    227 }
    228 
    229 PassRefPtr<KeyboardEventDispatchMediator> KeyboardEventDispatchMediator::create(PassRefPtr<KeyboardEvent> event)
    230 {
    231     return adoptRef(new KeyboardEventDispatchMediator(event));
    232 }
    233 
    234 KeyboardEventDispatchMediator::KeyboardEventDispatchMediator(PassRefPtr<KeyboardEvent> event)
    235     : EventDispatchMediator(event)
    236 {
    237 }
    238 
    239 bool KeyboardEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) const
    240 {
    241     // Make sure not to return true if we already took default action while handling the event.
    242     return EventDispatchMediator::dispatchEvent(dispatcher) && !event()->defaultHandled();
    243 }
    244 
    245 } // namespace WebCore
    246