OLD | NEW |
1 /** | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2001 Peter Kelly (pmk@post.com) | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de) | 3 // found in the LICENSE file. |
4 * Copyright (C) 2006 Samuel Weinig (sam.weinig@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 | 4 |
23 #include "sky/engine/config.h" | 5 #include "sky/engine/config.h" |
24 #include "sky/engine/core/events/KeyboardEvent.h" | 6 #include "sky/engine/core/events/KeyboardEvent.h" |
25 | 7 |
26 #include "sky/engine/platform/PlatformKeyboardEvent.h" | |
27 #include "sky/engine/platform/WindowsKeyboardCodes.h" | |
28 | |
29 namespace blink { | 8 namespace blink { |
30 | 9 |
31 static inline const AtomicString& eventTypeForKeyboardEventType(PlatformEvent::T
ype type) | 10 static AtomicString stringForType(WebInputEvent::Type type) |
32 { | 11 { |
33 switch (type) { | 12 if (type == WebInputEvent::KeyDown) |
34 case PlatformEvent::KeyUp: | 13 return EventTypeNames::keydown; |
35 return EventTypeNames::keyup; | 14 if (type == WebInputEvent::Char) |
36 case PlatformEvent::RawKeyDown: | 15 return EventTypeNames::keypress; |
37 return EventTypeNames::keydown; | 16 if (type == WebInputEvent::KeyUp) |
38 case PlatformEvent::Char: | 17 return EventTypeNames::keyup; |
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(); | 18 ASSERT_NOT_REACHED(); |
47 return EventTypeNames::keydown; | 19 return AtomicString(); |
48 } | 20 } |
49 | 21 |
50 static inline int windowsVirtualKeyCodeWithoutLocation(int keycode) | 22 static String locationFromModifiers(int modifiers) |
51 { | 23 { |
52 switch (keycode) { | 24 if (modifiers & WebInputEvent::IsKeyPad) |
53 case VK_LCONTROL: | 25 return "numpad"; |
54 case VK_RCONTROL: | 26 if (modifiers & WebInputEvent::IsLeft) |
55 return VK_CONTROL; | 27 return "left"; |
56 case VK_LSHIFT: | 28 if (modifiers & WebInputEvent::IsRight) |
57 case VK_RSHIFT: | 29 return "right"; |
58 return VK_SHIFT; | 30 return "standard"; |
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 PlatformKeybo
ardEvent& 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* vie
w) | |
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 KeyboardEventI
nit& 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 meta
Key) | |
125 : UIEventWithKeyState(eventType, canBubble, cancelable, view, 0, ctrlKey, al
tKey, shiftKey, metaKey) | |
126 , m_keyIdentifier(keyIdentifier) | |
127 , m_location(location) | |
128 , m_isAutoRepeat(false) | |
129 { | |
130 } | 31 } |
131 | 32 |
132 KeyboardEvent::~KeyboardEvent() | 33 KeyboardEvent::~KeyboardEvent() |
133 { | 34 { |
134 } | 35 } |
135 | 36 |
136 void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble,
bool cancelable, AbstractView* view, | |
137 const String &keyIdentifier, unsigned loca
tion, | |
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/2654
58): | |
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->windowsVirtualKe
yCode()); | |
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 | 37 const AtomicString& KeyboardEvent::interfaceName() const |
194 { | 38 { |
195 return EventNames::KeyboardEvent; | 39 return EventNames::KeyboardEvent; |
196 } | 40 } |
197 | 41 |
198 bool KeyboardEvent::isKeyboardEvent() const | 42 bool KeyboardEvent::isKeyboardEvent() const |
199 { | 43 { |
200 return true; | 44 return true; |
201 } | 45 } |
202 | 46 |
203 int KeyboardEvent::which() const | 47 KeyboardEvent::KeyboardEvent() |
204 { | 48 : KeyboardEvent(AtomicString(), KeyboardEventInit()) |
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 keyb
oard events. | |
207 return keyCode(); | |
208 } | |
209 | |
210 PassRefPtr<KeyboardEventDispatchMediator> KeyboardEventDispatchMediator::create(
PassRefPtr<KeyboardEvent> event) | |
211 { | |
212 return adoptRef(new KeyboardEventDispatchMediator(event)); | |
213 } | |
214 | |
215 KeyboardEventDispatchMediator::KeyboardEventDispatchMediator(PassRefPtr<Keyboard
Event> event) | |
216 : EventDispatchMediator(event) | |
217 { | 49 { |
218 } | 50 } |
219 | 51 |
220 bool KeyboardEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) c
onst | 52 KeyboardEvent::KeyboardEvent(const WebKeyboardEvent& event) |
| 53 : Event(stringForType(event.type), true, true) |
| 54 , m_key(event.key) |
| 55 , m_location(locationFromModifiers(event.modifiers)) |
| 56 , m_charCode(event.charCode) |
| 57 , m_ctrlKey(event.modifiers & WebInputEvent::ControlKey) |
| 58 , m_shiftKey(event.modifiers & WebInputEvent::ShiftKey) |
| 59 , m_altKey(event.modifiers & WebInputEvent::AltKey) |
| 60 , m_metaKey(event.modifiers & WebInputEvent::MetaKey) |
| 61 , m_repeat(event.modifiers & WebInputEvent::IsAutoRepeat) |
221 { | 62 { |
222 // Make sure not to return true if we already took default action while hand
ling the event. | 63 if (event.type == WebInputEvent::KeyDown || event.type == WebInputEvent::Key
Up) { |
223 return EventDispatchMediator::dispatchEvent(dispatcher) && !event()->default
Handled(); | 64 m_charCode = 0; |
| 65 } else if (event.type == WebInputEvent::Char) { |
| 66 m_key = 0; |
| 67 m_location = String(); |
| 68 } |
| 69 } |
| 70 |
| 71 KeyboardEvent::KeyboardEvent(const AtomicString& type, const KeyboardEventInit&
initializer) |
| 72 : Event(type, initializer) |
| 73 , m_key(initializer.key) |
| 74 , m_location(initializer.location) |
| 75 , m_charCode(initializer.charCode) |
| 76 , m_ctrlKey(initializer.ctrlKey) |
| 77 , m_shiftKey(initializer.shiftKey) |
| 78 , m_altKey(initializer.altKey) |
| 79 , m_metaKey(initializer.metaKey) |
| 80 , m_repeat(initializer.repeat) |
| 81 { |
224 } | 82 } |
225 | 83 |
226 } // namespace blink | 84 } // namespace blink |
OLD | NEW |