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. |
---|
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., 59 Temple Place - Suite 330, |
---|
20 | * Boston, MA 02111-1307, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "config.h" |
---|
24 | #include "KeyboardEvent.h" |
---|
25 | |
---|
26 | #include "EventNames.h" |
---|
27 | #include "PlatformKeyboardEvent.h" |
---|
28 | |
---|
29 | namespace WebCore { |
---|
30 | |
---|
31 | using namespace EventNames; |
---|
32 | |
---|
33 | KeyboardEvent::KeyboardEvent() |
---|
34 | : m_keyEvent(0) |
---|
35 | , m_keyLocation(DOM_KEY_LOCATION_STANDARD) |
---|
36 | , m_altGraphKey(false) |
---|
37 | { |
---|
38 | } |
---|
39 | |
---|
40 | KeyboardEvent::KeyboardEvent(const PlatformKeyboardEvent& key, AbstractView* view) |
---|
41 | : UIEventWithKeyState(key.isKeyUp() ? keyupEvent : key.isAutoRepeat() ? keypressEvent : keydownEvent, |
---|
42 | true, true, view, 0, key.ctrlKey(), key.altKey(), key.shiftKey(), key.metaKey()) |
---|
43 | , m_keyEvent(new PlatformKeyboardEvent(key)) |
---|
44 | , m_keyIdentifier(key.keyIdentifier()) |
---|
45 | , m_keyLocation(key.isKeypad() ? DOM_KEY_LOCATION_NUMPAD : DOM_KEY_LOCATION_STANDARD) |
---|
46 | , m_altGraphKey(false) |
---|
47 | { |
---|
48 | } |
---|
49 | |
---|
50 | KeyboardEvent::KeyboardEvent(const AtomicString& eventType, bool canBubble, bool cancelable, AbstractView *view, |
---|
51 | const String &keyIdentifier, unsigned keyLocation, |
---|
52 | bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey) |
---|
53 | : UIEventWithKeyState(eventType, canBubble, cancelable, view, 0, ctrlKey, altKey, shiftKey, metaKey) |
---|
54 | , m_keyEvent(0) |
---|
55 | , m_keyIdentifier(keyIdentifier) |
---|
56 | , m_keyLocation(keyLocation) |
---|
57 | , m_altGraphKey(altGraphKey) |
---|
58 | { |
---|
59 | } |
---|
60 | |
---|
61 | KeyboardEvent::~KeyboardEvent() |
---|
62 | { |
---|
63 | delete m_keyEvent; |
---|
64 | } |
---|
65 | |
---|
66 | void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view, |
---|
67 | const String &keyIdentifier, unsigned keyLocation, |
---|
68 | bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey) |
---|
69 | { |
---|
70 | if (dispatched()) |
---|
71 | return; |
---|
72 | |
---|
73 | initUIEvent(type, canBubble, cancelable, view, 0); |
---|
74 | |
---|
75 | m_keyIdentifier = keyIdentifier; |
---|
76 | m_keyLocation = keyLocation; |
---|
77 | m_ctrlKey = ctrlKey; |
---|
78 | m_shiftKey = shiftKey; |
---|
79 | m_altKey = altKey; |
---|
80 | m_metaKey = metaKey; |
---|
81 | m_altGraphKey = altGraphKey; |
---|
82 | } |
---|
83 | |
---|
84 | bool KeyboardEvent::getModifierState(const String& keyIdentifier) const |
---|
85 | { |
---|
86 | if (keyIdentifier == "Control") |
---|
87 | return ctrlKey(); |
---|
88 | if (keyIdentifier == "Shift") |
---|
89 | return shiftKey(); |
---|
90 | if (keyIdentifier == "Alt") |
---|
91 | return altKey(); |
---|
92 | if (keyIdentifier == "Meta") |
---|
93 | return metaKey(); |
---|
94 | return false; |
---|
95 | } |
---|
96 | |
---|
97 | int KeyboardEvent::keyCode() const |
---|
98 | { |
---|
99 | if (!m_keyEvent) |
---|
100 | return 0; |
---|
101 | if (type() == keydownEvent || type() == keyupEvent) |
---|
102 | return m_keyEvent->WindowsKeyCode(); |
---|
103 | return charCode(); |
---|
104 | } |
---|
105 | |
---|
106 | int KeyboardEvent::charCode() const |
---|
107 | { |
---|
108 | if (!m_keyEvent) |
---|
109 | return 0; |
---|
110 | String text = m_keyEvent->text(); |
---|
111 | if (text.length() != 1) |
---|
112 | return 0; |
---|
113 | return text[0]; |
---|
114 | } |
---|
115 | |
---|
116 | bool KeyboardEvent::isKeyboardEvent() const |
---|
117 | { |
---|
118 | return true; |
---|
119 | } |
---|
120 | |
---|
121 | int KeyboardEvent::which() const |
---|
122 | { |
---|
123 | // Netscape's "which" returns a virtual key code for keydown and keyup, and a character code for keypress. |
---|
124 | // That's exactly what IE's "keyCode" returns. So they are the same for keyboard events. |
---|
125 | return keyCode(); |
---|
126 | } |
---|
127 | |
---|
128 | KeyboardEvent* findKeyboardEvent(Event* event) |
---|
129 | { |
---|
130 | for (Event* e = event; e; e = e->underlyingEvent()) |
---|
131 | if (e->isKeyboardEvent()) |
---|
132 | return static_cast<KeyboardEvent*>(e); |
---|
133 | return 0; |
---|
134 | } |
---|
135 | |
---|
136 | } // namespace WebCore |
---|