2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / WebCore / dom / KeyboardEvent.cpp
blob6bc825fa212f879ac0c0e4aa3bb2d310852d9f98
1 /**
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.
23 #include "config.h"
24 #include "KeyboardEvent.h"
26 #include "Document.h"
27 #include "DOMWindow.h"
28 #include "EventNames.h"
29 #include "EventHandler.h"
30 #include "Frame.h"
31 #include "PlatformKeyboardEvent.h"
32 #include "Settings.h"
34 namespace WebCore {
36 static inline const AtomicString& eventTypeForKeyboardEventType(PlatformKeyboardEvent::Type type)
38 switch (type) {
39 case PlatformKeyboardEvent::KeyUp:
40 return eventNames().keyupEvent;
41 case PlatformKeyboardEvent::RawKeyDown:
42 return eventNames().keydownEvent;
43 case PlatformKeyboardEvent::Char:
44 return eventNames().keypressEvent;
45 case PlatformKeyboardEvent::KeyDown:
46 // The caller should disambiguate the combined event into RawKeyDown or Char events.
47 break;
49 ASSERT_NOT_REACHED();
50 return eventNames().keydownEvent;
53 KeyboardEvent::KeyboardEvent()
54 : m_keyEvent(0)
55 , m_keyLocation(DOM_KEY_LOCATION_STANDARD)
56 , m_altGraphKey(false)
60 KeyboardEvent::KeyboardEvent(const PlatformKeyboardEvent& key, AbstractView* view)
61 : UIEventWithKeyState(eventTypeForKeyboardEventType(key.type()),
62 true, true, view, 0, key.ctrlKey(), key.altKey(), key.shiftKey(), key.metaKey())
63 , m_keyEvent(new PlatformKeyboardEvent(key))
64 , m_keyIdentifier(key.keyIdentifier())
65 , m_keyLocation(key.isKeypad() ? DOM_KEY_LOCATION_NUMPAD : DOM_KEY_LOCATION_STANDARD) // FIXME: differentiate right/left, too
66 , m_altGraphKey(false)
70 KeyboardEvent::KeyboardEvent(const AtomicString& eventType, bool canBubble, bool cancelable, AbstractView *view,
71 const String &keyIdentifier, unsigned keyLocation,
72 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
73 : UIEventWithKeyState(eventType, canBubble, cancelable, view, 0, ctrlKey, altKey, shiftKey, metaKey)
74 , m_keyEvent(0)
75 , m_keyIdentifier(keyIdentifier)
76 , m_keyLocation(keyLocation)
77 , m_altGraphKey(altGraphKey)
81 KeyboardEvent::~KeyboardEvent()
83 delete m_keyEvent;
86 void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view,
87 const String &keyIdentifier, unsigned keyLocation,
88 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
90 if (dispatched())
91 return;
93 initUIEvent(type, canBubble, cancelable, view, 0);
95 m_keyIdentifier = keyIdentifier;
96 m_keyLocation = keyLocation;
97 m_ctrlKey = ctrlKey;
98 m_shiftKey = shiftKey;
99 m_altKey = altKey;
100 m_metaKey = metaKey;
101 m_altGraphKey = altGraphKey;
104 bool KeyboardEvent::getModifierState(const String& keyIdentifier) const
106 if (keyIdentifier == "Control")
107 return ctrlKey();
108 if (keyIdentifier == "Shift")
109 return shiftKey();
110 if (keyIdentifier == "Alt")
111 return altKey();
112 if (keyIdentifier == "Meta")
113 return metaKey();
114 return false;
117 int KeyboardEvent::keyCode() const
119 // IE: virtual key code for keyup/keydown, character code for keypress
120 // Firefox: virtual key code for keyup/keydown, zero for keypress
121 // We match IE.
122 if (!m_keyEvent)
123 return 0;
124 if (type() == eventNames().keydownEvent || type() == eventNames().keyupEvent)
125 return m_keyEvent->windowsVirtualKeyCode();
126 return charCode();
129 int KeyboardEvent::charCode() const
131 // IE: not supported
132 // Firefox: 0 for keydown/keyup events, character code for keypress
133 // We match Firefox, unless in backward compatibility mode, where we always return the character code.
134 bool backwardCompatibilityMode = false;
135 if (view())
136 backwardCompatibilityMode = view()->frame()->eventHandler()->needsKeyboardEventDisambiguationQuirks();
138 if (!m_keyEvent || (type() != eventNames().keypressEvent && !backwardCompatibilityMode))
139 return 0;
140 String text = m_keyEvent->text();
141 return static_cast<int>(text.characterStartingAt(0));
144 bool KeyboardEvent::isKeyboardEvent() const
146 return true;
149 int KeyboardEvent::which() const
151 // Netscape's "which" returns a virtual key code for keydown and keyup, and a character code for keypress.
152 // That's exactly what IE's "keyCode" returns. So they are the same for keyboard events.
153 return keyCode();
156 KeyboardEvent* findKeyboardEvent(Event* event)
158 for (Event* e = event; e; e = e->underlyingEvent())
159 if (e->isKeyboardEvent())
160 return static_cast<KeyboardEvent*>(e);
161 return 0;
164 } // namespace WebCore