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 static inline const AtomicString
& eventTypeForKeyboardEventType(PlatformKeyboardEvent::Type 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.
50 return eventNames().keydownEvent
;
53 KeyboardEvent::KeyboardEvent()
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
)
75 , m_keyIdentifier(keyIdentifier
)
76 , m_keyLocation(keyLocation
)
77 , m_altGraphKey(altGraphKey
)
81 KeyboardEvent::~KeyboardEvent()
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
)
93 initUIEvent(type
, canBubble
, cancelable
, view
, 0);
95 m_keyIdentifier
= keyIdentifier
;
96 m_keyLocation
= keyLocation
;
98 m_shiftKey
= shiftKey
;
101 m_altGraphKey
= altGraphKey
;
104 bool KeyboardEvent::getModifierState(const String
& keyIdentifier
) const
106 if (keyIdentifier
== "Control")
108 if (keyIdentifier
== "Shift")
110 if (keyIdentifier
== "Alt")
112 if (keyIdentifier
== "Meta")
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
124 if (type() == eventNames().keydownEvent
|| type() == eventNames().keyupEvent
)
125 return m_keyEvent
->windowsVirtualKeyCode();
129 int KeyboardEvent::charCode() const
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;
136 backwardCompatibilityMode
= view()->frame()->eventHandler()->needsKeyboardEventDisambiguationQuirks();
138 if (!m_keyEvent
|| (type() != eventNames().keypressEvent
&& !backwardCompatibilityMode
))
140 String text
= m_keyEvent
->text();
141 return static_cast<int>(text
.characterStartingAt(0));
144 bool KeyboardEvent::isKeyboardEvent() const
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.
156 KeyboardEvent
* findKeyboardEvent(Event
* event
)
158 for (Event
* e
= event
; e
; e
= e
->underlyingEvent())
159 if (e
->isKeyboardEvent())
160 return static_cast<KeyboardEvent
*>(e
);
164 } // namespace WebCore