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 "KeyboardEvent.h"
27 #include "DOMWindow.h"
28 #include "EventNames.h"
29 #include "EventHandler.h"
31 #include "PlatformKeyboardEvent.h"
36 using namespace EventNames;
38 static inline const AtomicString& eventTypeForKeyboardEventType(PlatformKeyboardEvent::Type type)
41 case PlatformKeyboardEvent::KeyUp:
43 case PlatformKeyboardEvent::RawKeyDown:
45 case PlatformKeyboardEvent::Char:
47 case PlatformKeyboardEvent::KeyDown:
48 // The caller should disambiguate the combined event into RawKeyDown or Char events.
55 KeyboardEvent::KeyboardEvent()
57 , m_keyLocation(DOM_KEY_LOCATION_STANDARD)
58 , m_altGraphKey(false)
62 KeyboardEvent::KeyboardEvent(const PlatformKeyboardEvent& key, AbstractView* view)
63 : UIEventWithKeyState(eventTypeForKeyboardEventType(key.type()),
64 true, true, view, 0, key.ctrlKey(), key.altKey(), key.shiftKey(), key.metaKey())
65 , m_keyEvent(new PlatformKeyboardEvent(key))
66 , m_keyIdentifier(key.keyIdentifier())
67 , m_keyLocation(key.isKeypad() ? DOM_KEY_LOCATION_NUMPAD : DOM_KEY_LOCATION_STANDARD) // FIXME: differentiate right/left, too
68 , m_altGraphKey(false)
72 KeyboardEvent::KeyboardEvent(const AtomicString& eventType, bool canBubble, bool cancelable, AbstractView *view,
73 const String &keyIdentifier, unsigned keyLocation,
74 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
75 : UIEventWithKeyState(eventType, canBubble, cancelable, view, 0, ctrlKey, altKey, shiftKey, metaKey)
77 , m_keyIdentifier(keyIdentifier)
78 , m_keyLocation(keyLocation)
79 , m_altGraphKey(altGraphKey)
83 KeyboardEvent::~KeyboardEvent()
88 void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view,
89 const String &keyIdentifier, unsigned keyLocation,
90 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
95 initUIEvent(type, canBubble, cancelable, view, 0);
97 m_keyIdentifier = keyIdentifier;
98 m_keyLocation = keyLocation;
100 m_shiftKey = shiftKey;
103 m_altGraphKey = altGraphKey;
106 bool KeyboardEvent::getModifierState(const String& keyIdentifier) const
108 if (keyIdentifier == "Control")
110 if (keyIdentifier == "Shift")
112 if (keyIdentifier == "Alt")
114 if (keyIdentifier == "Meta")
119 int KeyboardEvent::keyCode() const
121 // IE: virtual key code for keyup/keydown, character code for keypress
122 // Firefox: virtual key code for keyup/keydown, zero for keypress
126 if (type() == keydownEvent || type() == keyupEvent)
127 return m_keyEvent->windowsVirtualKeyCode();
131 int KeyboardEvent::charCode() const
134 // Firefox: 0 for keydown/keyup events, character code for keypress
135 // We match Firefox, unless in backward compatibility mode, where we always return the character code.
136 bool backwardCompatibilityMode = false;
138 backwardCompatibilityMode = view()->frame()->eventHandler()->needsKeyboardEventDisambiguationQuirks();
140 if (!m_keyEvent || (type() != keypressEvent && !backwardCompatibilityMode))
142 String text = m_keyEvent->text();
143 return static_cast<int>(text.characterStartingAt(0));
146 bool KeyboardEvent::isKeyboardEvent() const
151 int KeyboardEvent::which() const
153 // Netscape's "which" returns a virtual key code for keydown and keyup, and a character code for keypress.
154 // That's exactly what IE's "keyCode" returns. So they are the same for keyboard events.
158 KeyboardEvent* findKeyboardEvent(Event* event)
160 for (Event* e = event; e; e = e->underlyingEvent())
161 if (e->isKeyboardEvent())
162 return static_cast<KeyboardEvent*>(e);
166 } // namespace WebCore