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 "platform/PlatformKeyboardEvent.h"
     27 #include "platform/WindowsKeyboardCodes.h"
     28 
     29 namespace blink {
     30 
     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 
     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 
     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 
     88 KeyboardEventInit::KeyboardEventInit()
     89     : location(0)
     90     , ctrlKey(false)
     91     , altKey(false)
     92     , shiftKey(false)
     93     , metaKey(false)
     94     , repeat(false)
     95 {
     96 }
     97 
     98 KeyboardEvent::KeyboardEvent()
     99     : m_location(DOM_KEY_LOCATION_STANDARD)
    100     , m_isAutoRepeat(false)
    101 {
    102 }
    103 
    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())
    111 {
    112 }
    113 
    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)
    119 {
    120 }
    121 
    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)
    129 {
    130 }
    131 
    132 KeyboardEvent::~KeyboardEvent()
    133 {
    134 }
    135 
    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)
    139 {
    140     if (dispatched())
    141         return;
    142 
    143     initUIEvent(type, canBubble, cancelable, view, 0);
    144 
    145     m_keyIdentifier = keyIdentifier;
    146     m_location = location;
    147     m_ctrlKey = ctrlKey;
    148     m_shiftKey = shiftKey;
    149     m_altKey = altKey;
    150     m_metaKey = metaKey;
    151 }
    152 
    153 bool KeyboardEvent::getModifierState(const String& keyIdentifier) const
    154 {
    155     // FIXME: The following keyIdentifiers are not supported yet (crbug.com/265458):
    156     // "AltGraph", "CapsLock", "Fn", "NumLock", "ScrollLock", "SymbolLock", "OS".
    157     if (keyIdentifier == "Control")
    158         return ctrlKey();
    159     if (keyIdentifier == "Shift")
    160         return shiftKey();
    161     if (keyIdentifier == "Alt")
    162         return altKey();
    163     if (keyIdentifier == "Meta")
    164         return metaKey();
    165     return false;
    166 }
    167 
    168 int KeyboardEvent::keyCode() const
    169 {
    170     // IE: virtual key code for keyup/keydown, character code for keypress
    171     // Firefox: virtual key code for keyup/keydown, zero for keypress
    172     // We match IE.
    173     if (!m_keyEvent)
    174         return 0;
    175     if (type() == EventTypeNames::keydown || type() == EventTypeNames::keyup)
    176         return windowsVirtualKeyCodeWithoutLocation(m_keyEvent->windowsVirtualKeyCode());
    177 
    178     return charCode();
    179 }
    180 
    181 int KeyboardEvent::charCode() const
    182 {
    183     // IE: not supported
    184     // Firefox: 0 for keydown/keyup events, character code for keypress
    185     // We match Firefox
    186 
    187     if (!m_keyEvent || (type() != EventTypeNames::keypress))
    188         return 0;
    189     String text = m_keyEvent->text();
    190     return static_cast<int>(text.characterStartingAt(0));
    191 }
    192 
    193 const AtomicString& KeyboardEvent::interfaceName() const
    194 {
    195     return EventNames::KeyboardEvent;
    196 }
    197 
    198 bool KeyboardEvent::isKeyboardEvent() const
    199 {
    200     return true;
    201 }
    202 
    203 int KeyboardEvent::which() const
    204 {
    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.
    207     return keyCode();
    208 }
    209 
    210 void KeyboardEvent::trace(Visitor* visitor)
    211 {
    212     UIEventWithKeyState::trace(visitor);
    213 }
    214 
    215 PassRefPtrWillBeRawPtr<KeyboardEventDispatchMediator> KeyboardEventDispatchMediator::create(PassRefPtrWillBeRawPtr<KeyboardEvent> event)
    216 {
    217     return adoptRefWillBeNoop(new KeyboardEventDispatchMediator(event));
    218 }
    219 
    220 KeyboardEventDispatchMediator::KeyboardEventDispatchMediator(PassRefPtrWillBeRawPtr<KeyboardEvent> event)
    221     : EventDispatchMediator(event)
    222 {
    223 }
    224 
    225 bool KeyboardEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) const
    226 {
    227     // Make sure not to return true if we already took default action while handling the event.
    228     return EventDispatchMediator::dispatchEvent(dispatcher) && !event()->defaultHandled();
    229 }
    230 
    231 } // namespace blink
    232